Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Storing the value of a variable from one observation in a local macro or scalar

    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);

  • #2

    Code:
    local x1 = make[1] 
    
    scalar x2 = make[1]

    Comment


    • #3
      Hi Nick,
      Thank you for your response, but I'm sorry I wasn't more clear. I need to be able to use the -if- (or some alternative since if doesn't work) when defining my local macro and scalar because I don't know the observation number for my actual data.
      For a scalar, I realize I could use the summarize function and store the mean in the scalar x2 using r(mean) (see the code below).
      But this approach does not work for a string variable.
      I'd appreciate your advice.
      Best,
      Brent

      Code:
      . sysuse auto
      (1978 Automobile Data)
      
      . summarize price if make=="AMC Concord"
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
             price |          1        4099           .       4099       4099
      
      . scalar x2 = r(mean)
      
      . di scalar(x2)
      4099
      Last edited by Brent Fulton; 22 May 2022, 16:40.

      Comment


      • #4
        If the value is distinct, you can use -levelsof-

        Code:
        sysuse auto, clear
        levelsof make if price==5705, local(wanted) clean
        di "`wanted'"
        Res.:

        Code:
        . levelsof make if price==5705, local(wanted) clean
        Chev. Impala
        
        . 
        . di "`wanted'"
        Chev. Impala

        Comment


        • #5
          Thank you, Andrew, that works.
          Best,
          Brent

          Comment

          Working...
          X