Announcement

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

  • Shortcut for multiple variables

    Hi all, I assume this is a very simple question (to which the answer might be 'no'), but is there an easy way for referring to a sequence of variables in my scenario...my code would be something like

    Code:
    egen newvar = rowmax(df9d_w1 df10d_w1 df11d_w1 df12d_w1)
    Is there a way to specify that range of just the number in the middle of the variable name, without reordering the variables? There are currently other variables in between them. If the solution is just to reorder the variables, it's probably easier for me to just list them all out instead of adding additional lines of code.

  • #2
    The answer depends on whatever other variables you have. For example,

    Code:
    d df1?d_w1
    will reveal whether

    Code:
     
     egen newvar = rowmax(df9d_w1 df1?d_w1)
    would work, as catching the desired variables only, but as you say, it's easiest (and clearest) just to name those four variables explicitly.


    Comment


    • #3
      Thanks Nick. In a case where it did work, what would be the shortest solution to this slightly different variation in code? Something like:

      Code:
      replace newvar = 0 if (df9d_w1 - df15d_w1) == 0

      I think that would be right?

      Comment


      • #4
        That's legal (if and only if all variables named do exist and are numeric) but it calculates the difference between df9d_w1 and df15d_w1 and tests whether that difference is zero.

        So, - is here the minus sign as usual and this code has nothing to do with variable list ranges or wildcards.
        Last edited by Nick Cox; 16 Aug 2022, 11:47.

        Comment


        • #5
          Ah yes, legal but not actually what I wanted to do. Is there another way to specify "if [any in this range of variables] == 0"? Sort of like the opposite of an inrange()?

          Comment


          • #6
            There are such functions within egen.

            Comment

            Working...
            X