Announcement

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

  • Accessing lookfor results in for loop

    I need to use the stored results (in r()) from lookfor to go thru the list of variables and tabulate those with zero or missing values. I know this isn't that hard, but I tried all of my hack guesses and have gone thru the Goog results and can't figure this out. Any help appreciated.

    Bill

  • #2
    Code:
    sysuse auto, clear
    lookfor t
    local vars_with_t_in_their_name `r(varlist)'
    ...
    do_something_with `vars_with_t_in_their_name'
    More generally, when you don't know what Stata calls something that it returns in -r()-, just run the command and then run -return list-. Stata will then list out everything stored in -r()- accompanied by its name. For example:
    Code:
    . sysuse auto
    (1978 automobile data)
    
    . lookfor t
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ----------------------------------------------------------------------------------------------------------------------------------------------
    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
    
    . return list
    
    macros:
                r(varlist) : "trunk weight length turn displacement gear_ratio"
    
    .

    Comment


    • #3
      As I understand it, lookfor won't help you find variables with zero or missing values unless somehow that is mentioned in their names or variable labels.

      However, findname from the Stata Journal can do this:

      Code:
      webuse nlswork, clear 
      
      . findname , any(missing(@))
      age       nev_mar   not_smsa  south     occ_code  wks_ue    hours
      msp       grade     c_city    ind_code  union     tenure    wks_work
      
      . findname , any(@ == 0)
      msp       grade     not_smsa  south     wks_ue    tenure    ln_wage
      nev_mar   collgrad  c_city    union     ttl_exp   wks_work
      
      . findname , any(@ == 0 | missing(@))
      age       nev_mar   collgrad  c_city    ind_code  union     ttl_exp   hours     ln_wage
      msp       grade     not_smsa  south     occ_code  wks_ue    tenure    wks_work

      Comment


      • #4
        Many thanks Clyde.

        Comment

        Working...
        X