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

Hi...I am trying to create the variable Intake where the values are either 'Initial' or 'Continued'. The criteria is for each ID, if the Year is the first Year for the ID, then Intake should be 'Initial' otherwise Intake should be 'Continued'. Can't seem to get the output wanted. Any suggestions? Thanks.

 

 

data Have;
input ID Year;
datalines;
1 2020
1 2021
1 2022
2 2020
2 2020
2 2020
3 2020
3 2020
3 2021
3 2021
;

data Want;
length Intake $ 9;
set Have;
by ID Year;
retain Intake1 'Continued' Intake2 'Initial'; 
	if first.ID and first.Year then 
		Intake = Intake2; 
	else
	if not first.ID and Year = lag(Year) then
		Intake = lag(Intake);
	else 
	Intake = Intake1;
	if missing(Intake) then 
		Intake = Intake1;
run;

Get:
Intake	ID	Year	Intake1	Intake2
Initial	1	2020	Continued	Initial
Continued	1	2021	Continued	Initial
Continued	1	2022	Continued	Initial
Initial	2	2020	Continued	Initial
Continued	2	2020	Continued	Initial
Continued	2	2020	Continued	Initial
Initial	3	2020	Continued	Initial
Continued	3	2020	Continued	Initial
Continued	3	2021	Continued	Initial
Continued	3	2021	Continued	Initial


Want:
Intake	ID	Year	Intake1	Intake2
Initial 1	2020	Continued	Initial
Continued	1	2021	Continued	Initial
Continued	1	2022	Continued	Initial
Initial	2	2020	Continued	Initial
Initial	2	2020	Continued	Initial
Initial	2	2020	Continued	Initial
Initial	3	2020	Continued	Initial
Initial	3	2020	Continued	Initial
Continued	3	2021	Continued	Initial
Continued	3	2021	Continued	Initial
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming your data set is already sorted, this should do the trick:

data want ;
   set have ;
   length intake $ 9;
   by id year ;
   if first.id then intake="Initial  " ;
   else if first.year then intake="Continued" ;
   retain intake;
run;

View solution in original post

4 REPLIES 4
AMSAS
SAS Super FREQ

Either I don't understand your request or your want dataset is wrong

 

Is this what you are looking for?

 

data Have;
input ID Year;
datalines;
1 2020
1 2021
1 2022
2 2020
2 2020
2 2020
3 2020
3 2020
3 2021
3 2021
;

proc sort data=have out=srtd ;
	by id year ;
run ;

data want ;
	set srtd ;
	by id year ;
	if first.year then
		intake="Initial  " ;
	else
		intake="Continued" ;
run ;
twildone
Pyrite | Level 9

Hi AMSAS....Thanks for your suggestion. When I run your code, I get column intake. I need the output as in column Intake(Want). Thanks

 

ID Year intake Intake (Want)
1 2020 Initial Initial
1 2021 Initial Continued
1 2022 Initial Continued
2 2020 Initial Initial
2 2020 Continued Initial
2 2020 Continued Initial
3 2020 Initial Initial
3 2020 Continued Initial
3 2021 Initial Continued
3 2021 Continued Continued
Astounding
PROC Star

Assuming your data set is already sorted, this should do the trick:

data want ;
   set have ;
   length intake $ 9;
   by id year ;
   if first.id then intake="Initial  " ;
   else if first.year then intake="Continued" ;
   retain intake;
run;
twildone
Pyrite | Level 9

Hi Astounding. Perfect...it did the trick...thanks a ton.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1004 views
  • 2 likes
  • 3 in conversation