Announcement

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

  • Markers across months

    Hi all,

    As always, thanks in advance for your help.

    I have a dataset (see below) showing participation in activities, with a unique ID (CWIN) and a variable for each month showing the number of activities. I need to create (1) a single variable marking which IDs were active for the first one OR two months AND then inactive and (2) a single variable marking which ids had one activity the whole time and also were enrolled in another program (details don't matter) for six months (dummy variable sixmonths)

    for problem number one, this is the code I tried
    gen partial = (TotalActivities* == 0) /// this errors out. I guess you can't use the asterisk in this syntax. I could brute force it, I have few enough variables, but I'd like to learn about a better way if it exists
    replace partial = 1 if TotalActivities1 >= 1
    replace partial = 1 if TotalActivites2 >= 1

    for problem number two, I thought maybe I needed a for loop
    gen oneact=0
    forvalues i=1/6 {
    replace oneact= 1 if sixmonths == 1 & TotalActivities`i'==1
    }

    This resulted in a hit for anyone who had a "1" for ANY of the months, not ALL of the months. Maybe I just need another step where I make a variable showing both cases, but again, curious if there's a more elegant solution.

    Thanks so much!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double ID float(TotalActivities1 TotalActivities2 TotalActivities3 TotalActivities4 TotalActivities5 TotalActivities6 sixmonths)
     615 . . . . . . 1
     828 . . . . . . 1
    1033 1 2 1 1 1 1 0
    2318 . . . . . . 1
    2320 4 4 3 3 3 3 0
    2538 1 1 1 . . . 1
    2682 . . . 1 1 1 1
    2795 1 1 1 1 1 1 0
    3226 . . . . . . 1
    3243 2 1 1 1 1 1 0
    3707 1 1 1 . . . 1
    3974 1 1 1 1 1 1 0
    4038 . . . 1 . . 1
    4621 1 1 1 1 . . 1
    5272 . . . . . . 1
    5437 2 2 2 2 3 3 0
    5513 . . . 1 1 1 1
    5961 . . . . . . 1
    6148 . 1 1 1 . . 1
    6189 2 2 2 2 3 2 0
    end

    Listed 20 out of 3136 observations

  • #2
    One small trick here is to just to concatenate the history into a string -- which won't work easily if the number is ever 10 or more. For example


    Code:
    . egen History = concat(TotalActivities*)
    
    . 
    . list ID if inlist(History, "11....", "1.....") .

    Comment


    • #3
      Oh neat, yeah, I didn't know that was possible... should be easier to do the brute force method if I do that and use inlist. Thank you so much!

      Comment

      Working...
      X