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

Hello,

 

I am trying to manipulate the data by ID, so that the variable "start" always takes the value of lag "end", except for the first value which is 0. The "end" variable also has to be adjusted accordingly. Please see attached what I mean.

 

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @blackandwhite 

 

You can try this. Let me know if that does correspond to what you want.

Best,

 

data want;
	set have;
	
	by ID;
	
	dif = end - start;
	
	if first.ID then end2 = 0;
	end2 + dif;
	
	start2 = lag(end2);
	if first.ID then start2 = 0;

	drop start end dif;
	rename start2 = start end2 = end;
	
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Please supply example data in a data step with datalines, as you have been shown in https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m... 

Excel files can not convey important attributes of SAS data, and make it harder for us to recreate your data. Data steps are unambiguous.

blackandwhite
Obsidian | Level 7

Ok - will do next time, thanks.

ed_sas_member
Meteorite | Level 14

Hi @blackandwhite 

 

You can try this. Let me know if that does correspond to what you want.

Best,

 

data want;
	set have;
	
	by ID;
	
	dif = end - start;
	
	if first.ID then end2 = 0;
	end2 + dif;
	
	start2 = lag(end2);
	if first.ID then start2 = 0;

	drop start end dif;
	rename start2 = start end2 = end;
	
run;
blackandwhite
Obsidian | Level 7

works great- thanks very much!!

novinosrin
Tourmaline | Level 20

Hi @blackandwhite  Sorry late to the party as I missed to see this question. Oh well.

 

If i understand correctly, one temp variable gymnastic alone should do

 


data have;
input ID	start	end;
cards;
1	0	6
1	11	17
1	17	25
2	0	5
2	5	10
2	10	18
;

data want;
set have;
by id;
end=ifn(not first.id,sum(_iorc_,end-start,0),end);
start=ifn(not first.id,_iorc_,start);
_iorc_=end;
run;
blackandwhite
Obsidian | Level 7

Hi @novinosrin, works great, thanks very much.

Can't complain about having several options 🙂

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 6 replies
  • 1580 views
  • 2 likes
  • 4 in conversation