Announcement

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

  • Refer to multiple variables in Stata

    Hi,

    My variables' names are something like var11, var11_oth, var12, var12_oth, var13, var13_oth.etc... up to 12. All of variables ending with "_oth" are in string format; and the others are numeric values.
    I want to create a new variable which is the sum of all numeric values only. My code was like:
    egen=rowtotal (var*)

    But it will include the string variables also.
    Is there any other way of refering to numeric variables without typing one by one?

    Thank you so much,

    Lanna

  • #2
    Code:
    ds *_oth , not
    will leave in r(varlist) a list of all variable names that do not end in _oth. An alternative is

    Code:
    ds , has(type numeric)
    which will put all numeric variables into r(varlist).


    For essentially the same approach with slightly different syntax, see findname (SJ):

    Cox, N. J. 2020. Update: Finding variable names. Stata Journal volume 20, number 4
    -- 2015. Update: Finding variable names. Stata Journal volume 15, number 2
    -- Cox, N. J. 2012. Update: Finding variable names. Stata Journal volume 12, number 1
    -- Cox, N. J. 2010a. Update: Finding variable names. Stata Journal volume 10, number 4
    -- Cox, N. J. 2010b. Speaking Stata: Finding variables. Stata Journal volume 10, number 2

    Comment


    • #3
      Apart what Daniel suggested above, you can read more on wildcards in Stata, * is not the only wildcard. You should also get in the habit when asking such questions to provide a sample of your data using -dataex-.

      The following might or might not do the trick, as I do not have a sample of your data I do not know for sure:

      Code:
      egen=rowtotal (var??)

      Comment


      • #4
        Thank Joro and Daniel. It is really helpful!!!

        Comment

        Working...
        X