DATA Step, Macro, Functions and more

Creating a Variable from multiple variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Creating a Variable from multiple variables

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


Accepted Solutions
Solution
‎04-22-2017 10:39 PM
Respected Advisor
Posts: 4,920

Re: Creating a Variable from multiple variables

data ad44;
set ad45;
time = max(of MosLater1-MosLater14);
run;
PG

View solution in original post


All Replies
Occasional Contributor
Posts: 13

Re: Creating a Variable from multiple variables

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

Solution
‎04-22-2017 10:39 PM
Respected Advisor
Posts: 4,920

Re: Creating a Variable from multiple variables

data ad44;
set ad45;
time = max(of MosLater1-MosLater14);
run;
PG
Respected Advisor
Posts: 4,173

Re: Creating a Variable from multiple variables

[ Edited ]

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

Occasional Contributor
Posts: 13

Re: Creating a Variable from multiple variables

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!
Super User
Posts: 19,789

Re: Creating a Variable from multiple variables

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

 

time2 = coalesce(of MosLater14-MosLater1);
Super User
Posts: 19,789

Re: Creating a Variable from multiple variables

[ Edited ]

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. 

☑ This topic is solved.

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

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