SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Adding start and stop variables based on number of days

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Adding start and stop variables based on number of days

I have a dataset in the form of 

id total_days
A60
A90
A60
B40
B20
B90
B70

 

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_daysstartstop
A60160
A9061150
A60151210

 

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;


Accepted Solutions
Solution
‎04-20-2018 05:48 PM
Super User
Posts: 10,689

Re: Adding start and stop variables based on number of days

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;

View solution in original post


All Replies
PROC Star
Posts: 1,584

Re: Adding start and stop variables based on number of days

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;
Super User
Posts: 13,321

Re: Adding start and stop variables based on number of days

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;
Esteemed Advisor
Posts: 5,482

Re: Adding start and stop variables based on number of days

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;
PG
Solution
‎04-20-2018 05:48 PM
Super User
Posts: 10,689

Re: Adding start and stop variables based on number of days

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;
Contributor
Posts: 22

Re: Adding start and stop variables based on number of days

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?

Super User
Posts: 10,689

Re: Adding start and stop variables based on number of days

 

/*******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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 182 views
  • 1 like
  • 5 in conversation