Announcement

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

  • generating new variable that sums many variables

    Dear Statalisters

    I have 25 variables all named event_eventtype , where event type may differ.
    I then have an additional 25 variables named event_eventtype180, event type can be both_sides or oneside - the naming seems a bit random.
    I then have an additional 25 variables named event_eventtype365

    all variables are integer 0,1,2,3,4…_N


    i want to generate a new variable that is the sum of all 25 event_eventtype variables
    and one that is the sum of all event_eventtype180
    and one that is the sum of all event_eventtype365

    the slow and space filling way is to copy all variable names of interest into the do editor and enter + between the variables, but i imagine there is a better solution to my problem.


    Hope this makes sens

    thank you

    lars

  • #2
    Does the following pseudo-code help? (1) Use ds to get a list of all your "event_eventtype" variables. (2) note that ds leaves behind the varlist in r(varlist). (3) Feed this macro containing the varlist to egen: egen newvar = rowtotal(`r(varlist)')

    Comment


    • #3
      ds is a wonderful command. But it may be that

      Code:
       
      describe *side*180 
      describe *side*365
      shows that a wildcard will work in each case.

      Comment


      • #4
        but is there a way to leave out variables that ends in 180 and 365 from the ds r(varlist)?

        Nick,
        I seemed to have left out that the _eventname is random i only selected 2 to show that there were different names, it can be up, down, right, left and all other directions really.

        lars

        Comment


        • #5
          ds has a not option.

          You will thus need more than one wildcard.

          Comment


          • #6
            yes, but how do i use the option?
            My variables are all named
            event_aneventname
            event_aneventname180
            event_aneventname365

            and the not option does not seem to allow variable names as i get an error message saying: event_ option not allowed when i go:
            Code:
            ds event_*, not event_*180 event_*365
            and further more, I'm not sure how to do the rowtotal when i only want the *180's or *365's because i cannot see how i leave out the event_aneventname variables.

            Lars

            Comment


            • #7
              ds, not is a bit counter-intuitive (in my opinion). If your variables are frog toad newt cat caterpillar, and you want to return frog toad newt, you can use ds cat*, not. ds cat* will return cat and caterpillar, and the not option will 'invert' the varlist to return the remaining variables. You don't actually specify any variables with/after the not option.

              Comment


              • #8
                Another technique is

                Code:
                unab all: event_* 
                unab A : event_*365 
                unab B : event_*180 
                local some : list all - A 
                local some : list some - B 
                di "`some'"

                Comment


                • #9
                  There might be an easier way to do this with egen and rowtotal with wildcards. Something like

                  egen a=rowtotal(event_*)
                  egen a=rowtotal(event_*180)

                  Comment


                  • #10
                    But Phil,
                    egen a=rowtotal(event_*), will include all variables both event_*, event_*180 AND event_*365,
                    I am interested in generating
                    all= sum of all event_* (not including event_*180 or event_*365)
                    180= sum of all event_*180 (not including all or event_*365)
                    365= sum of all event_*365 (not including all or 180)

                    I have a slight log on issue this morning so i cannot test the above given examples.

                    Thank you anyway.

                    Lars

                    Comment


                    • #11
                      I think you have all the techniques you need.

                      Comment


                      • #12
                        It worked out fine, thank you.

                        Comment

                        Working...
                        X