I have a dataset in the form of
id | total_days |
A | 60 |
A | 90 |
A | 60 |
B | 40 |
B | 20 |
B | 90 |
B | 70 |
I want to add two variables whose values would represent the start and stop days of each row. For instance, for id A, it would be
id | total_days | start | stop |
A | 60 | 1 | 60 |
A | 90 | 61 | 150 |
A | 60 | 151 | 210 |
I used the following code and was fairly confident that it would give me what I was looking for. However, the start and stop variables were populated only in the first row of each id and were missing in other rows. I am unable to understand why the codes did not work.
data cum_days;
set num_days;
by id;
if first.id then do;
start=1;
stop=total_days;
end;
else do;
start=lag(stop) + 1;
stop= start + total_days -1;
end;
run;
data have;
input id $ total_days;
datalines;
A 60
A 90
A 60
B 40
B 20
B 90
B 70
;
data want;
set have;
by id;
if first.id then stop=0;
stop+total_days;
start=lag(stop)+1;
if first.id then start=1;
run;
data have;
input id $ total_days;
datalines;
A 60
A 90
A 60
B 40
B 20
B 90
B 70
;
data want;
set have;
by id;
retain start stop;
if first.id then do; start=1;stop=total_days;end;
else do;start=stop+1;stop+total_days;end;
run;
The issue is the LAG function queue behavior.
You might consider RETAINing the value of Stop
data want; set have; by id; Retain stop; if first.id then do; start=1; stop=total_days; end; else do; start=stop + 1; stop= start + total_days -1; end; run;
No lag, no retain required with a DO UNTIL() loop:
data have;
input id $ total_days;
datalines;
A 60
A 90
A 60
B 40
B 20
B 90
B 70
;
data want;
stop = 0;
do until (last.id);
set have; by id;
start = stop + 1;
stop + total_days;
output;
end;
run;
proc print data=want noobs; var id total_days start stop; run;
data have;
input id $ total_days;
datalines;
A 60
A 90
A 60
B 40
B 20
B 90
B 70
;
data want;
set have;
by id;
if first.id then stop=0;
stop+total_days;
start=lag(stop)+1;
if first.id then start=1;
run;
Thank you very much! I would also like to understand how your set of codes were processed differently than mine. It would be of much help if you could walk me through your codes, so that I would be able to solve similar issues on my own in future. Also, what was the issue with my codes?
/*******stop is cumulative sum of total_days, I think you understand it.***********/
if first.id then stop=0; /*since cum is happened in a group,so set it be zero at the first obs.*/
stop+total_days;
/*it is easy to understand. get lag value of stop and + 1 as start variable*/
start=lag(stop)+1;
/*when reaching the first obs of a group, start would be the last stop of the previous group*/
/*Therefore, set it be 1 manually*/
if first.id then start=1;
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.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.