DATA Step, Macro, Functions and more

Add X days to each subsequent each row, by group

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Add X days to each subsequent each row, by group

Hi everyone, 

 

I am stuck on a data management task and thought someone here may be able to help me. 

 

I am working with a dataset where each individual has ~30 rows of repeated data. The variables with information that differs are dates and a variable that indicates the first for each group. I'd like to add 7 number of days to every row, by group. I have a start date for the first row for each individual and need to add 7 days to each row until the next individual is reached (e.g., row 31) where the same process is repeated. Any suggestions? Here is what I have and what i want the data to look like. Thank you!! 

 

data have; 
	input id date first;
	datalines;
	1 03OCT2010 1
	1         . 0
	1         . 0
	1         . 0
	2 01JAN2011 1
	2         . 0
	2         . 0
	;
run; 

data want; 
	input id date first;
	datalines; 
	1 03OCT2010 1
	1 10OCT2010 0
	1 17OCT2010 0
	1 24OCT2010 0
	2 01JAN2011 1
 	2 08JAN2011 0
	2 15JAN2011 0
	;
run; 

 

 

 


Accepted Solutions
Solution
‎09-22-2017 11:22 AM
Trusted Advisor
Posts: 1,389

Re: Add X days to each subsequent each row, by group

Posted in reply to pamplemouse22

As long as FLAG=1 for the first record of each id, this will work:

 

data want;
  set have;
  retain tmpdate 0;
  tmpdate=ifn(flag=1,date,tmpdate+7);
  if flag^=1 then date=tmpdate;
  drop tmpdate;
run;

View solution in original post


All Replies
Solution
‎09-22-2017 11:22 AM
Trusted Advisor
Posts: 1,389

Re: Add X days to each subsequent each row, by group

Posted in reply to pamplemouse22

As long as FLAG=1 for the first record of each id, this will work:

 

data want;
  set have;
  retain tmpdate 0;
  tmpdate=ifn(flag=1,date,tmpdate+7);
  if flag^=1 then date=tmpdate;
  drop tmpdate;
run;
Contributor
Posts: 39

Re: Add X days to each subsequent each row, by group

This works beautifully! Thank you. 

 

I'm trying to understand how this works. 

What does the carrot in following line do?

flag^=1

 

Super User
Posts: 13,913

Re: Add X days to each subsequent each row, by group

Posted in reply to pamplemouse22

pamplemouse22 wrote:

This works beautifully! Thank you. 

 

I'm trying to understand how this works. 

What does the carrot in following line do?

flag^=1

 


^=   is "not equal" . There are some other ways depending on different keyboards/operating systems that are also acceptable.

so

Flag not equal to 1

Contributor
Posts: 39

Re: Add X days to each subsequent each row, by group

oh, hah! that makes sense. thank you!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 196 views
  • 0 likes
  • 3 in conversation