Dear Statalist,

I'm using Stata 16.0 trying to store a variable value from one observation in a local macro (if the variable is a string) or in a scalar (if the variable is numeric).

Please see my example below. I cannot get the code to work because Stata does not allow "if" when defining locals and scalars. I tried also tried to use the list command (e.g., list make if price=4099), but was not sure how to store the output of the list command.

I'd appreciate your advice.

Thanks,
Brent



Code:
. *Here's an example
. sysuse auto
(1978 Automobile Data)

. 
. *In the first observation, the make variable is "AMC Concord" and the price variable is 4,099
. list make price if _n==1

     +---------------------+
     | make          price |
     |---------------------|
  1. | AMC Concord   4,099 |
     +---------------------+

. 
. *I would like to store AMC Concord in the local macro x, but this command doesn't work
. local x1 = make if price==4099
if not allowed
r(101);

end of do-file

r(101);

. do "C:\Users\fultonb\AppData\Local\Temp\STD627c_000000.tmp"

. *Also, I would like to store price in a scalar, but this command doesn't work
. scalar x2 = price if make=="AMC Concord"
if not allowed
r(101);

end of do-file

r(101);