Announcement

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

  • create local macro w very long VarList

    I am recoding lots of variables and wonder if there is a simpler way of having long variables lists with a single local macro. I need to reuse (and edit) these variable lists so I am putting them in separate do-files then calling them as needed with the include command.

    Is there a way to have the var names in the list be wrapped across multiple compact lines of text -- instead of strung out across the right-hand side of the editor window in a single line?
    • I am using Stata v14 on Windows 7 sys
    • The vars I am working w are predominantly nonconsecutive, therefore it is not helpful to use conventions such as var1-var99, or var1*.
    • One suggestion I found is in Long's "Workflow of Data Analysis" , where he nests the local macro name within itself multiple times -- like this:
    Code:
    local macroVarList  "var1 var12 var34 var42 var50"
    
    local macroVarList  " `macroVarList'  var61 var78 var82 var93"
    
    local macroVarList  " `macroVarList'  var101 var111 var123 var132"
    Is there any other simple way to write the long VarList (60-90 vars) for a local macro so that it wraps across shorter lines so it is easier to deal with?

    Much thanks in advance,
    wg
    Cheers, wg
    ~ ~
    sapere aude ~~

  • #2
    You can break up a macro definition over multiple lines the same way you can any other Stata command.

    Code:
    local demo    1    2    3    4     ///
    5    6    7    8     ///
    9    10    11    12
                
    display `"`demo'"'
    In the above code, I used tab characters to separate all the items in the list from each other and from the ///'s. But you can just use blanks if you prefer.

    Comment


    • #3
      Wendy,

      I see at least two strategies possible:

      1) write a very long macro as you show by citing every variable by name;
      2) construct the list by inspecting the variables and picking the 'eligible' ones.

      First one is not very interesting. You have a working solution from Scott Long that you display and it is a good solution. For readability I'd probably reduce the macro name from macroVarlist to mv or something short like that, easier to retype in 20 times without making a typo. Some other strategies are discussed here: http://www.statalist.org/forums/foru...inside-strings

      Second is more interesting, but requires the definition of eligible. It could be trivial if it is "all string variables", "all variables with all values missing", etc, or it could be next to impossible "all variables that I need for my analysis".

      In general, variables may be marked for selection through variable characteristics, and later picked up via the ds command. See details here: http://www.stata.com/manuals13/dds.pdf

      Finally, you can also use an external text editor with line wrapping, like notepad.exe or pretty much anything else; or alternatively, place the list of variables into an external files and iterate over that file. It all depends on whether the list of variables is known and fixed, or it is dynamic and is a result of some other analysis.

      Best regards, Sergiy

      Comment


      • #4
        Initially I tried that but I am getting an error -- after reading only the first line:

        Code:
        local  AGREE  "AG072  AG082  AG086  AG087  AG089  AG090  AG093  AG099  AG134  AG150  AG155   ///
        AG157  AG161  AG180  AG181  AG185  AG187  AG200  AG201  AG202  AG207  AG221   ///
        AG285  AG292  AG293  AG294  AG296  AG297" 
        
        display `AGREE'
        
        
        . local AGREE "AG072  AG082  AG086  AG087  AG089  AG090  AG093  AG099  AG134  AG150  AG155     ///
        invalid syntax
        r(198);
        
        end of do-file
        Cheers, wg
        ~ ~
        sapere aude ~~

        Comment


        • #5
          Eliminate the quotation marks from the beginning and end of the list. You can interrupt a local macro definition with ///, but you can't do that with a string literal within quotes. You don't need the quotes, and they are getting in your way here.

          Comment


          • #6
            One way I often do this is to temporarily change the delimiter, then you can wrap however you want. For example:

            Code:
            #delimit ;
            local varlist
               var1
               var2
               var3
               <etc>
              ;
            #delimit cr

            hth,
            Jeph

            Comment


            • #7
              Thanks for all your suggestions. I have made a habit of enclosing my VarLists w quotes, that I forgot that I dont have to do this. I feel a bit silly that I did not realize the quotes were causing this problem for me today. Thanks to Clyde for reminding me of that ;-)
              Cheers, wg
              ~ ~
              sapere aude ~~

              Comment

              Working...
              X