Announcement

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

  • Functions that can take lists of variables

    If I've counted correctly, there are no functions in Stata that can take varlists as arguments, but there are six functions in Stata that can take what are effectively varlists:
    1. max()
    2. min()
    3. missing()
    4. inlist() (with the max argument size caveat)
    5. recode()
    6. irrecode()
    Of these, it seems like it would be handy if the first four could accept actual varlists, and I'm thinking about making that feature request. Before I do, is there a fundamental limitation of functions that would preclude this? Or a reason that this would be undesirable?

  • #2
    If a function can take an expression as argument, then it can understand a subtraction sign as such. That clashes with one kind of varlist. So in the first case x-y means a difference, and in the second case all the variables from x to y.

    I believe that clash to be decisive here.

    Several egen functions can work on varlists

    Comment


    • #3
      Yes, that seems fatal. Excellent point, thanks. Probably not worth introducing variants of these functions just to avoid that issue. Tragic!

      egen is a great replacement for gen, but it doesn't work everywhere else you might want to use a function.

      Comment


      • #4
        It’s slightly more typing but perfectly readable to precede with the one liner call to have Stata expand the varlist, and possibly one more to introduce intervening commas, if necessary.

        Comment


        • #5
          Building on Leonardo Guizzetti 's suggestion in #4, it can be done in one line and then fed to the function call. You don't need an extra command to introduce the comma separation: -ds-'s -separate()- option will handle that.

          Code:
          ds varlist, clean separate(", ")
          ... function(`r(varlist)') ...

          Comment


          • #6
            In my version 19.5 installation, I don't find "clean" or "separate" as documented or undocumented options for -ds-. I could sure use them right now :-}. Is there some other command that was intended?

            Comment


            • #7
              My bad. -ds- doesn't have those options and can't be used for this purpose. I was thinking of -levelsof()- which does produce nice comma-separated lists of values of a variable. But that won't help here. Sorry. Egg on my face. #5 is retracted.

              Comment


              • #8
                You can wrap what's essentially a one-liner into a program
                Code:
                program csvarlist , rclass
                    
                    version 16.1
                    
                    syntax varlist
                    
                    return local varlist = subinstr("`varlist'"," ",",",.)
                    
                end
                then
                Code:
                csvarlist varlist
                ... fcn(`r(varlist)')
                or skip the wrapper and simply type
                Code:
                unab varlist : varlist
                ... function(subinstr("`varlist'"," ",",",.))
                Last edited by daniel klein; 29 Oct 2025, 01:55.

                Comment


                • #9
                  Clyde Schechter Been there! I was thinking that StataCorp must have added some new options to ds, which I helped to write, since I last looked carefully at the help. Not so....

                  daniel klein has a nice solution. I imagine he would agree that there is a delicate trade-off between having a one-liner (whose command name has to be remembered, and which must be installed and accessible) and being able to write a few lines with official syntax.

                  One very experienced user I know seemingly never uses egen, claiming (I dare say accurately in their case) that they can usually create code for any function wanted with other code faster than it takes to find out whether such a function exists and its exact syntax.

                  Here are three more ideas. Naturally you can, and usually will, work with subsets of variables.

                  Code:
                  . sysuse auto
                  (1978 automobile data)
                  1. Fire up unab
                  .
                  Code:
                  . unab all1 : *
                  
                  . local all1 : subinstr local all1 " " ",", all
                  2. Do what ds can do.

                  Code:
                  . ds
                  make          mpg           headroom      weight        turn          gear_ratio
                  price         rep78         trunk         length        displacement  foreign
                  
                  . local all2 `r(varlist)'
                  
                  . local all2 : subinstr local all2 " " ",", all
                  3. findname A while back I came to dislike syntax I introduced to ds and have wanted to keep adding features intermittently since.

                  Code:
                  . search findname , sj
                  
                  Search of official help files, FAQs, Examples, and Stata Journals
                  
                  SJ-23-4 dm0048_5  . . . . . . . . . . . . . . . . Software update for findname
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q4/23   SJ 23(4):1096
                          options vallabelcountdef() and vallabelcountuse() have been
                          extended to allow zero (0) as an argument
                  
                  SJ-20-2 dm0048_4  . . . . . . . . . . . . . . . . Software update for findname
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q2/20   SJ 20(2):504
                          new options include columns()
                  
                  SJ-15-2 dm0048_3  . . . . . . . . . . . . . . . . Software update for findname
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q2/15   SJ 15(2):605--606
                          updated to be able to find strL variables
                  
                  SJ-12-1 dm0048_2  . . . . . . . . . . . . . . . . Software update for findname
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q1/12   SJ 12(1):167
                          correction for handling embedded double quote characters
                  
                  SJ-10-4 dm0048_1  . . . . . . . . . . . . . . . . Software update for findname
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q4/10   SJ 10(4):691
                          update for not option
                  
                  SJ-10-2 dm0048  . . . . . . . . . . . . . .  Speaking Stata: Finding variables
                          (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                          Q2/10   SJ 10(2):281--296
                          produces a list of variable names showing which variables
                          have specific properties, such as being of string type, or
                          having value labels attached, or having a date format
                  
                  (end of search)
                  
                  .
                  . findname, local(all3)
                  make          mpg           headroom      weight        turn          gear_ratio
                  price         rep78         trunk         length        displacement  foreign
                  
                  . local all3 : subinstr local all3 " " ",", all
                  Output below has been edited to remove irrelevant macros.
                  .
                  Code:
                  . mac li
                  
                  _all3:          make,price,mpg,rep78,headroom,trunk,weight,length,turn,displacement,gear_r
                                  > atio,foreign
                  _all2:          make,price,mpg,rep78,headroom,trunk,weight,length,turn,displacement,gear_r
                                  > atio,foreign
                  _all1:          make,price,mpg,rep78,headroom,trunk,weight,length,turn,displacement,gear_r
                                  > atio,foreign
                  I am tempted to add an option to findname to add commas to the local macro. It won't be the solution everyone prefers, but that's fine.

                  Comment


                  • #10
                    I added a cslocal() option to findname. That will produce output of a comma-separated list of variable names in a local macro. The existing local() option is unaffected and may be specified too.

                    The updated command is now on SSC, thanks to KitBaum as ever. (The version there was outdated in any case.)

                    As mentioned in #9 the version on the Stata Journal website is from 2023. A formal Update in the Stata Journal will appear in due course. For now, the SSC version is more up-to-date.

                    Comment


                    • #11
                      As explained at https://www.statalist.org/forums/for...updated-on-ssc the update is now also available from the Stata Journal website.

                      Comment

                      Working...
                      X