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
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.
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.
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;
Thank you, Reeza.
Luckily you answered both my questions. Can you briefly show me how to use by and not split the files?
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.