Announcement

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

  • Help writing a program that runs a for loop using a varlist

    I am trying to write a program in Stata. The goal of the program is to loop through a varlist. I have my code below and some notes about the program. I am running Stata version 7.

    Code:
    local pain arthralgia_pain back_pain headache_pain autoimmune_pain other_msk_pain fibro_pain inflam_pain neuro_pain
    
    program table_out
    args i j varlist describe `varlist', varlist // this step is part of troubleshooting. Only headache is displayed di `i' // this is fine di `j' // this is fine di `varlist' //nothing display foreach var of local `varlist' { local j = `j' + 1 local i = `i' + 1 di `var' // a period is displayed }
    end quietly describe `pain', varlist table_out `i' `j' `pain'
    I would appreciate any help.
    Laura

  • #2
    There are several problems here.

    First, he command -args i j varlist- is syntactically legal, but it does not do what you think it does. It does not match up varlist with a variable list. -args- can only assign single tokens (constants, single variable names). To submit an entire varlist as arguments to a program you write, you must use the -syntax- command.

    Next, when you write -di `varlist'- you will not get a listing of the variable names in varlist. -display- interprets a variable name argument as a request to show the value of that variable in the first observation of the data in memory. So the . output you got suggests that the first observation of that variable was a missing value. If you want -display- to show the name of a variable, you have to bind that name in quotes.

    Your loop keeps augmenting the values of i and j, but you never actually use those, so maybe those are unnecessary commands and arguments? Or maybe you are just showing a very simplified version of your program and in the real thing you do make use of them.

    Finally, your call to program table_out is incorrect. You have not defined local macros i and j at that point in the program, so `i' and `j' are both reduced to empty strings. Your command therefore expands as -table_out arthralgia_pain back_pain headache_pain autoimmune_pain other_msk_pain fibro_pain inflam_pain neuro_pain-. When you then ask Stata to -describe `varlist'- in your original code, `varlist' is the content of the third token (because of -args i j varlist-) in this list, namely headache_pain. So that's what you get.

    Here's a fixed up version. You didn't provide any example data to test this with. So I just used the auto.dta and renamed the variables to be similar to yours.
    Code:
    sysuse auto, clear
    rename price arthralgia_pain
    rename mpg back_pain
    rename headroom headache_pain
    rename rep78 autoimmune_pain
    rename trunk other_msk_pain
    rename weight fibro_pain
    rename length inflam_pain
    rename turn neuro_pain
    
    local pain arthralgia_pain back_pain headache_pain autoimmune_pain other_msk_pain fibro_pain inflam_pain neuro_pain
    
    capture program drop table_out
    program table_out
        syntax varlist, i(integer) j(integer) // FIXED
        describe `varlist', varlist   // FIXED
        return list // ADDED TO VERIFY THAT THE varlist OPTION OF -describe- WORKED
        di `i'  // this is fine
        di `j' // this is fine
        di "`varlist'"  // FIXED
        foreach var of local varlist {
            local j = `j' + 1
            local i = `i' + 1
        di `"`var'"' // FIXED
    
        }
    
    end
    
    table_out `pain', i(3) j(5) // FIXED
    Note: Version 7 is extremely old. I made these changes in the current version, 18. The commands used here are all very simple ones that, I think, were present in version 7 and worked the same way they do now. But I don't have version 7 around anymore, so I can't verify that. I did precede all this code with a -version 7- command. That often restores older behavior--but not always. So it is possible that something I have done here will be unacceptable to version 7, or will behave differently. There isn't anything I can do about that.

    Comment


    • #3
      Just a small comment on syntax and args. You can have multiple tokens/words/arguments with args; you just need to use double quotes for binding:

      Code:
      program arg_in_double_quotes
          
          args foo bar foobar
          
          display "`foo'"
          display "`bar'"
          display "`foobar'"
          
      end
      can be called as

      Code:
      . arg_in_double_quotes foo bar "foobar also_shown" not_shown
      foo
      bar
      foobar also_shown
      So, syntax is not necessarily needed.

      Comment


      • #4
        Ah, yes. I didn't think about putting a varlist in quotes for this purpose. Still, I think using -syntax- is a better way to do this in this case.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Still, I think using -syntax- is a better way to do this in this case.
          I agree.

          Comment

          Working...
          X