Announcement

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

  • Selecting a subset of ordered variables without typing them manually

    Dear all,

    I am trying to execute a fairly simple operation:

    Code:
    egen duration_desk=rowtotal(t_*_pagesubmit)

    The issue I have is that with the code I just showed, I am selecting all my variables that include 't_*_pagesubmit' in their name, and I am only interested in a few of them that luckly are ordered. However, in-between there are other non-related variables that do not follow the 't_*_pagesubmit' formula.

    Say I am interested in variables 't_1_pagesubmit' 't_2_pagesubmit' and so on until 't_10_pagesubmit'. I could use a code that selects from 1 to 10 but how can I do that without selecting the variables in-between them?

    Of course I could simply type all the variables manually but there must be a simpler way around.
    I do apologize if this has been answered, I honestly did not know how to search for such a problem.
    Thank you for your time.

  • #2
    Have a look at
    [U] 11.4 varname and varlists.

    Comment


    • #3
      I can think of two sorts of things that can help with knotty problems in identifying the right variable list:
      1. if the variables to be included (or excluded) can be identified using some characteristics, you might want to use the -ds- command and pick up the local it stores in r(varlist). See
        Code:
        help ds
      2. You can also use the flexibility of macro lists (perhaps in combination with the above suggestion) to do set operations on these lists. See
        Code:
        help macro_lists
        .

      Comment


      • #4
        If the * in

        Code:
         t_*_pagesubmit 
        is catching more than 1 2 3 4 5 6 7 8 9 10 then ideally we need to know what that more is to suggest a better approach. But


        Code:
        forval j = 1/10 {      
             local vars `vars' t_`j'_pagesubmit
        }    
        
        egen duration_desk=rowtotal(`vars')


        is one more general solution.
        Last edited by Nick Cox; 29 Aug 2022, 04:55.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          If the * in

          Code:
          t_*_pagesubmit 
          is catching more than 1 2 3 4 5 6 7 8 9 10 then ideally we need to know what that more is to suggest a better approach. But
          Code:
          forval j = 1/10 { local vars `vars' t_`j'_pagesubmit
          Code:
          }
          egen duration_desk=rowtotal(`vars')
          is one more general solution.
          Thanks, that sounds like a solution but indeed there is more. I just simplified it for the example but the * is basically the name of the variables so there is no common trend. One can be 'f130200' and another 'deviceuse_h'.

          Comment


          • #6
            You have lost me now. There is no way that a wildcard like

            Code:
             t_*_pagesubmit
            will catch either of f130200 or deviceuse_h. Other way round, if #1 doesn't epitomize your real problem, what does?

            Comment


            • #7
              Originally posted by Nick Cox View Post
              You have lost me now. There is no way that a wildcard like

              Code:
              t_*_pagesubmit
              will catch either of f130200 or deviceuse_h. Other way round, if #1 doesn't epitomize your real problem, what does?
              Isn't there a way to say select t_*_pagesubmit from t_f130200_pagesubmit to t_deviceuse_h_pagesubmit for example?

              If not then I guess I'll have to do a macro and that's it.

              Comment


              • #8
                I see what you want now. it's the intersection of two sets,

                Code:
                unab A : t_*_pagesubmit
                unab B : t_f130200_pagesubmit-t_device_h_pagesubmit
                local wanted : list A & B
                A, B and wanted are here all local macros.

                It is possible that

                Code:
                t_?_pagesubmit t_10_pagesubmit 
                is what you want but in principle the ? could match many single characters not 1(1)9.
                Last edited by Nick Cox; 29 Aug 2022, 06:23.

                Comment

                Working...
                X