BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fengyuwuzu
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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.

Reeza
Super User

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;
fengyuwuzu
Pyrite | Level 9

Thank you, Reeza.

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

 

 

Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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