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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21
data ad44;
set ad45;
time = max(of MosLater1-MosLater14);
run;
PG

View solution in original post

6 REPLIES 6
chavens
Fluorite | Level 6

In a different datastep, I already created the variable time via the code: time=MosLater14;

PGStats
Opal | Level 21
data ad44;
set ad45;
time = max(of MosLater1-MosLater14);
run;
PG
Patrick
Opal | Level 21

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).

chavens
Fluorite | Level 6
I did mean to pick the biggest of the MosLater variable. If MosLater5 was the first missing then 6-14 would also be missing (and also 3<4<5<6...etc). I just could not think of how to do that initially, thus the confusing and inefficient code.

Thanks!
Reeza
Super User

Using Patricks code, but you can skip the array declaration, unless you're using it elsewhere.

 

time2 = coalesce(of MosLater14-MosLater1);
Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1109 views
  • 7 likes
  • 4 in conversation