Announcement

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

  • Select all variables based on suffix

    I want to select all variables that contain a certain suffix to store them in a macro. Any suggestions?

    MWE:
    Code:
    sysuse auto.dta
    
    ds make, not 
    foreach var of varlist `r(varlist)' {
        gen `var'_minus_one = `var' - 1
    }
    
    * This does not work
    global vars *_minus_one

  • #2
    I am sure that it does what it should. I suspect that you are reaching for something more like


    Code:
    unab vars : *_minus_one
    This stores the names of all the variables concerned (not please, the variables) separately, but in a local macro.

    Comment


    • #3
      That is what unab is for. It returns the results in a local instead of a global macro, but I would consider that an advantage: globals should not be used unless absolutely necessary, which is almost never.

      Code:
      . sysuse auto.dta, clear
      (1978 Automobile Data)
      
      .
      . ds, has(type numeric)
      price         rep78         trunk         length        displacement  foreign
      mpg           headroom      weight        turn          gear_ratio
      
      . foreach var of varlist `r(varlist)' {
        2.     gen `var'_minus_one = `var' - 1
        3. }
      (5 missing values generated)
      
      .
      . * This does not work
      . unab vars : *_minus_one
      
      . di "`vars'"
      price_minus_one mpg_minus_one rep78_minus_one headroom_minus_one trunk_minus_one weight_minus_one length_minus_one turn_min
      > us_one displacement_minus_one gear_ratio_minus_one foreign_minus_one
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        That's what I was looking for, thank you!

        Comment

        Working...
        X