Hi All,
I'm trying to get the following code to work where I basically want to create the variable "time" from the largest "MosLater" variable for each participant, but the code that I'm using is just producing all blanks for some reason when creating the time variable (no errors in the log though)... Any ideas on how to get this to work?
data ad44;
set ad45;
if MosLater14=. then time=MosLater13;
else if MosLater13=. then time=MosLater12;
else if MosLater12=. then time=MosLater11;
else if MosLater11=. then time=MosLater10;
else if MosLater10=. then time=MosLater9;
else if MosLater9=. then time=MosLater8;
else if MosLater8=. then time=MosLater7;
else if MosLater7=. then time=MosLater6;
else if MosLater6=. then time=MosLater5;
else if MosLater5=. then time=MosLater4;
else if MosLater4=. then time=MosLater3;
else if MosLater3=. then time=MosLater2;
else if MosLater2=. then time=MosLater1;
run;
Thanks,
Cody
In a different datastep, I already created the variable time via the code: time=MosLater14;
data ad44;
set ad45;
time = max(of MosLater1-MosLater14);
run;
Given your narrative and especially your code it looks to mee that you don't want to pick the largest value from the MosLater variables (that's what @PGStats code does) but you want to pick the first non-missing value from a variable list ordered descending from 14 to 1 (MosLater14 - MosLater1). That's what below code does.
data have;
array MosLater {14};
do _i=1 to dim(MosLater);
MosLater[_i]=ceil(ranuni(1)*100);
output;
end;
drop _i;
stop;
run;
data want;
length time 8.;
set have;
time = coalesce(of MosLater14-MosLater1);
run;
Change history:
Code amended based on @Reeza's comment (removal of array statement).
Using Patricks code, but you can skip the array declaration, unless you're using it elsewhere.
time2 = coalesce(of MosLater14-MosLater1);
Coalesce()?
Edit: by largest do you mean the largest value across or the largest MOS variable? It appears the latter according to your code but the former according to your question.
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!
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.