Announcement

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

  • Generate variable based on variable labels

    Hi, I'm trying to generate a new variable and realized that one efficient way to do it might be using variable labels, because the variable names are not informative. However, I wasn't able to find any good answers or example code on this after a search--especially because I was not the one to create the variable labels, they were already included in the dataset given to me.

    This is the general gist of what I am looking to do:

    Code:
    gen visible = 1 if ffb*_w3 [variable label contains the word "seen"]

  • #2
    You can use ds to identify such variables. The variables are stored in the macro `r(varlist)'. Below, say I want the row sum of variables that have "in" (inches) in their variable labels.

    Code:
    sysuse auto, clear
    desc
    ds, has(varlab *in*)
    egen wanted = rowtotal(`r(varlist)')
    l headroom length displacement wanted in 1/3
    Res.:

    Code:
     desc
    
    Contains data from \ado\base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:            12                          13 Apr 2018 17:45
                                                  (_dta has notes)
    
    -----------------------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    -----------------------------------------------------------------------------------------------------------------
    make            str18   %-18s                 Make and Model
    price           int     %8.0gc                Price
    mpg             int     %8.0g                 Mileage (mpg)
    rep78           int     %8.0g                 Repair Record 1978
    headroom        float   %6.1f                 Headroom (in.)
    trunk           int     %8.0g                 Trunk space (cu. ft.)
    weight          int     %8.0gc                Weight (lbs.)
    length          int     %8.0g                 Length (in.)
    turn            int     %8.0g                 Turn Circle (ft.)
    displacement    int     %8.0g                 Displacement (cu. in.)
    gear_ratio      float   %6.2f                 Gear Ratio
    foreign         byte    %8.0g      origin     Car type
    -----------------------------------------------------------------------------------------------------------------
    Sorted by: foreign
    
    .
    . ds, has(varlab *in*)
    headroom      length        displacement
    
    .
    . egen wanted = rowtotal(`r(varlist)')
    
    . l headroom length displacement wanted in 1/3
    
         +---------------------------------------+
         | headroom   length   displa~t   wanted |
         |---------------------------------------|
      1. |      2.5      186        121    309.5 |
      2. |      3.0      173        258      434 |
      3. |      3.0      168        121      292 |
         +---------------------------------------+

    Comment


    • #3
      Thanks Andrew Musau

      Comment

      Working...
      X