BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
finans_sas
Quartz | Level 8

Hi Everyone:

 

I posted below an example data set along with a column that I would like to create. In this data set, I have different people (personid) and for some observations, an event is present (1) and for some it is not (0). The data are sorted by personid and date, which I did not show. I want to create a new column (want):

-whenever, event=0, it should be 0.

-for a given personid: for the first event=1, it should be 1, and should stay as 1 as long as event=1.

-for a given personid: each time, the event transitions from 0 to 1, its value increases by 1.

 

I do not know how exactly I should go about this. I would very much appreciate your help.

Thank you

 

personid event want
1    0    0
1    0    0
1    0    0
1    0    0
1    0    0
1    1    1
1    1    1
1    1    1
1    1    1
1    1    1
1    0    0
1    0    0
1    1    2
1    1    2
1    1    2
1    0    0
1    1    3
2    1    1
2    1    1
2    0    0
2    0    0
2    0    0
2    0    0
2    1    2
2    1    2
2    1    2
2    0    0
2    0    0
2    1    3
2    1    3
2    1    3
2    0    0
2    1    4

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

The following will do what you want:

data want (drop=_:);
  set have;
  by personid;
  _last_event=ifn(not first.personid,lag(event),0);
  if first.personid then _hold=0;
  if event eq 1 and _last_event eq 0 then _hold+1;
  want=ifn(event eq 0,0,_hold);
run;

Art, CEO, AnalystFinder.com

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

The following will do what you want:

data want (drop=_:);
  set have;
  by personid;
  _last_event=ifn(not first.personid,lag(event),0);
  if first.personid then _hold=0;
  if event eq 1 and _last_event eq 0 then _hold+1;
  want=ifn(event eq 0,0,_hold);
run;

Art, CEO, AnalystFinder.com

Astounding
PROC Star

One possibility:

 

data want;

set have;

by personid event notsorted;

if first.personid then event_counter=0;

if first.event and event=1 then event_counter + 1;

if event=0 then want=0;

else want = event_counter;

drop event_counter;

run;

 

finans_sas
Quartz | Level 8

Thank you so much for your help! I love this forum.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 761 views
  • 2 likes
  • 3 in conversation