I want the following PROC SQL code to be run for multiple variables. Till now, it is designed for a single variable i.e. Balance. I want this to be run for multiple variables in a loop. The code calculates ratio and total sum of balance for a time period.
Data Sets used in the code -
data example1; input ID Months Revenue Balance; cards; 101 1 3 90 101 2 33 68 101 3 22 51 101 4 3 90 101 5 33 65 101 6 22 54 101 7 3 92 101 8 33 65 101 9 22 55 101 10 3 96 101 11 33 65 101 12 22 54 102 1 100 18 102 2 58 62 102 3 95 97 102 4 100 18 102 5 58 65 102 6 95 92 102 7 100 11 102 8 58 62 102 9 95 92 102 10 100 15 102 11 58 60 102 12 95 91 ;
data trend; input x y; cards; 1 3 4 6 1 6 7 9 1 9 10 12 1 12 ; run;
proc sql noprint; select cat("sum(case when Months =",y," then Balance else . end)", "/ sum(case when Months =",x," then Balance else . end) as Balance_",y,"_",x,"_ratio") into :ratio separated by "," from trend; select cat("sum(case when ", x,"<= Months <=",y, " then Balance else . end) as Balance_",x,"_",y,"_Sum") into :loop separated by "," from trend; create table Output_trend as select ID, &ratio., &loop., from example1 group by ID; quit;
For example : Variables are specified in the following "vars" macro paramer and then run the above PROC SQL code for the variables mentioned in the macro variable.
%let vars = Balance Revenue; %let n=%sysfunc(countw(&vars,%str( )));
... View more