DATA Step, Macro, Functions and more

how to make a do loop for running the same code multiple times for different variable ?

Accepted Solution Solved
Reply
Super Contributor
Posts: 312
Accepted Solution

how to make a do loop for running the same code multiple times for different variable ?

[ Edited ]

I need to run the same code for 5 variables, and each variable 4 times, so total 20 times:

 

 

 

%macro scatter(var=,p=);
data test;
set percent.&var._&p;
ID = _n_;  /* have to create a ID variable as x-axis variable for scatter plot purpose */
run;
proc sgplot data=test;
scatter x=ID y=&var;
title "&var  &p";
run;
%mend scatter;
%scatter(var=betting_days,p=p90)
%scatter(var=betting_days,p=p95)
%scatter(var=betting_days,p=p98)
%scatter(var=betting_days,p=p99)

%scatter(var=total_bet_times,p=p90)
%scatter(var=total_bet_times,p=p95)
%scatter(var=total_bet_times,p=p98)
%scatter(var=total_bet_times,p=p99)

%scatter(var=max_wager,p=p90)
%scatter(var=max_wager,p=p95)
%scatter(var=max_wager,p=p98)
%scatter(var=max_wager,p=p99)

%scatter(var=sum_wager,p=p90)
%scatter(var=sum_wager,p=p95)
%scatter(var=sum_wager,p=p98)
%scatter(var=sum_wager,p=p99)

%scatter(var=aver_wager_day,p=p90)
%scatter(var=aver_wager_day,p=p95)
%scatter(var=aver_wager_day,p=p98)
%scatter(var=aver_wager_day,p=p99)

 

 

 

Is there a way to be more efficient, i.e. no need to run 20 times?

 

Thanks


Accepted Solutions
Solution
‎08-05-2016 03:49 PM
Super User
Posts: 5,082

Re: how to make a do loop for running the same code multiple times for different variable ?

It depends on what you mean by "more efficient".  The macro will run 20 times.  But you can automate some of that, so that the programming workload is lower. 

 

I'm going to hard-code the levels of 90, 95, 98, 99.  That too can be automated ... but of course that requires more complex programming.

 

%macro run_scatter (varlist=);

   %local i nextvar;

   %do i=1 %to %sysfunc(countw(&varlist));

      %let nextvar = %scan(&varlist, &i);

      %scatter (var=&nextvar, p=p90)

      %scatter (var=&nextvar, p=p95)

      %scatter (var=&nextvar, p=p98)

      %scatter (var=&nextvar, p=p99)

   %end;

%mend run_scatter;

 

It's untested code, but assuming there are no errors, all you need to do is call the macro:

 

%run_scatter (varlist=betting_days total_bet_times max_wager sum_wager aver_wager_day)

 

Pass any list of variable names that should be plotted.

View solution in original post


All Replies
Solution
‎08-05-2016 03:49 PM
Super User
Posts: 5,082

Re: how to make a do loop for running the same code multiple times for different variable ?

It depends on what you mean by "more efficient".  The macro will run 20 times.  But you can automate some of that, so that the programming workload is lower. 

 

I'm going to hard-code the levels of 90, 95, 98, 99.  That too can be automated ... but of course that requires more complex programming.

 

%macro run_scatter (varlist=);

   %local i nextvar;

   %do i=1 %to %sysfunc(countw(&varlist));

      %let nextvar = %scan(&varlist, &i);

      %scatter (var=&nextvar, p=p90)

      %scatter (var=&nextvar, p=p95)

      %scatter (var=&nextvar, p=p98)

      %scatter (var=&nextvar, p=p99)

   %end;

%mend run_scatter;

 

It's untested code, but assuming there are no errors, all you need to do is call the macro:

 

%run_scatter (varlist=betting_days total_bet_times max_wager sum_wager aver_wager_day)

 

Pass any list of variable names that should be plotted.

Super User
Posts: 17,829

Re: how to make a do loop for running the same code multiple times for different variable ?

This is great example of why you shouldn't split data up. You then end up doing things like this rather than using BY groups.

 

Given your current situation I would recommend a call execute within a data step. Untested:

 

data macro_call;
do var='betting_days', 'total_bet_times', 'max_wager', 'sum_wager';
do p='p90', 'p95', 'p98','p99';

str=catt(
    '%scatter(var=', 
    var, 
    ', p=', 
    p, 
   ')'
         );

call execute(str);

end;
end;
run;
Super Contributor
Posts: 312

Re: how to make a do loop for running the same code multiple times for different variable ?

Thank you, Reeza.

Luckily you answered both my questions. Can you briefly show me how to use by and not split the files?

 

 

Super User
Posts: 17,829

Re: how to make a do loop for running the same code multiple times for different variable ?

Transpose so variables are long and use BY. Here's a quick sketch. You can add in the percentile part by adding another BY variable.

 

data flipped;
set data;
array vars(*) betting_days total_time average_bat;

do i=1 to dim(vars)
var=vname(vars(i));
value=vars(i);
output;
end;

drop betting_days total_time average_bat;

run;

proc sort data=flipped;
by var;
run;

proc sgplot data=flipped;
title '#byval(vars) by ID';
by vars;
scatter x=id y=value;
run;
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 418 views
  • 2 likes
  • 3 in conversation