Hello
In my raw data I have information of failure for each customer.(Each customer has one row).
For each customer there is a potential follow up of 15 months and we check If he/she touch failure at this period.
Some of the customers didn't complete the full 15 months of following up periods.
My question:
I need to write 15 IF statements.
What is the better way to do it?(More clever way)?
It also might happen in the future that I need to run it on following up of different period...so maybe it is better to add a macro var
%let k=15;
that define the potential follow up period and then the IF statements will be adjusted automatic.
Another question, Do you think that in this case I need to use "ELSE IF" statements or "IF"?(I know that result will be same but I want to ask what do you think)
IF No_Months_Exposed=15 then Ind_Touch_Failure=MAX(OF grade1-grade15);
ELSE IF No_Months_Exposed=14 then Ind_Touch_Failure=MAX(OF grade2-grade15);
ELSE IF No_Months_Exposed=13 then Ind_Touch_Failure=MAX(OF grade3-grade15);
ELSE IF No_Months_Exposed=12 then Ind_Touch_Failure=MAX(OF grade4-grade15);
ELSE IF No_Months_Exposed=11 then Ind_Touch_Failure=MAX(OF grade5-grade15);
ELSE IF No_Months_Exposed=10 then Ind_Touch_Failure=MAX(OF grade6-grade15);
ELSE IF No_Months_Exposed=9 then Ind_Touch_Failure=MAX(OF grade7-grade15);
ELSE IF No_Months_Exposed=8 then Ind_Touch_Failure=MAX(OF grade8-grade15);
ELSE IF No_Months_Exposed=7 then Ind_Touch_Failure=MAX(OF grade9-grade15);
ELSE IF No_Months_Exposed=6 then Ind_Touch_Failure=MAX(OF grade10-grade15);
ELSE IF No_Months_Exposed=5 then Ind_Touch_Failure=MAX(OF grade11-grade15);
ELSE IF No_Months_Exposed=4 then Ind_Touch_Failure=MAX(OF grade12-grade15);
ELSE IF No_Months_Exposed=3 then Ind_Touch_Failure=MAX(OF grade13-grade15);
ELSE IF No_Months_Exposed=2 then Ind_Touch_Failure=MAX(OF grade14-grade15);
ELSE IF No_Months_Exposed=1 then Ind_Touch_Failure=MAX(OF grade15-grade15);
%macro t;
%do i=15 %to 1 %by -1;
%let t= %eval(15-&i+1);
%if &i<15 %then else ;
if No_Months_Exposed=&i then Ind_Touch_Failure=MAX(OF grade&t-grade15);
%end;
%mend t;
options mprint;
/*Call the macro*/
data want;
set have;
%t;
run;
117 options mprint;
118 /*Call the macro*/
119 data want;
120 set have;
121 %t;
MPRINT(T): if No_Months_Exposed=15 then Ind_Touch_Failure=MAX(OF grade1-grade15);
MPRINT(T): else if No_Months_Exposed=14 then Ind_Touch_Failure=MAX(OF grade2-grade15);
MPRINT(T): else if No_Months_Exposed=13 then Ind_Touch_Failure=MAX(OF grade3-grade15);
MPRINT(T): else if No_Months_Exposed=12 then Ind_Touch_Failure=MAX(OF grade4-grade15);
MPRINT(T): else if No_Months_Exposed=11 then Ind_Touch_Failure=MAX(OF grade5-grade15);
MPRINT(T): else if No_Months_Exposed=10 then Ind_Touch_Failure=MAX(OF grade6-grade15);
MPRINT(T): else if No_Months_Exposed=9 then Ind_Touch_Failure=MAX(OF grade7-grade15);
MPRINT(T): else if No_Months_Exposed=8 then Ind_Touch_Failure=MAX(OF grade8-grade15);
MPRINT(T): else if No_Months_Exposed=7 then Ind_Touch_Failure=MAX(OF grade9-grade15);
MPRINT(T): else if No_Months_Exposed=6 then Ind_Touch_Failure=MAX(OF grade10-grade15);
MPRINT(T): else if No_Months_Exposed=5 then Ind_Touch_Failure=MAX(OF grade11-grade15);
MPRINT(T): else if No_Months_Exposed=4 then Ind_Touch_Failure=MAX(OF grade12-grade15);
MPRINT(T): else if No_Months_Exposed=3 then Ind_Touch_Failure=MAX(OF grade13-grade15);
MPRINT(T): else if No_Months_Exposed=2 then Ind_Touch_Failure=MAX(OF grade14-grade15);
MPRINT(T): else if No_Months_Exposed=1 then Ind_Touch_Failure=MAX(OF grade15-grade15);
122 run;
Why not use an array to eliminate the need for the wallpaper code and hence any macro logic to generate the wallpaper code.
array grade [15];
Ind_Touch_Failure=.;
do index=(15-No_Months_Exposed+1) to 15 ;
Ind_Touch_Failure=max(Ind_Touch_Failure,grade[index]);
end;
@Tom - Love that term wallpaper code! Must add that to my lexicon.
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.