Dear SAS folks,
I have more than 400 variables and want check the distribution of data for each of them. I developed a piece of code and found that it didn't work. The code is as following:
%macro test;
%array abc{406} &var.;
%do i=1 %to 406;
proc sql;
create table abc{i} as
select abc{i}
from monthend.final_201509
;
quit;
%end;
%mend;
Any suggestions?
Thanks,
Harry
What do you mean by check the distribution?
Why not something as simple as below. It generates all the summaries for all numeric variables in the dataset by default.
proc univariate data=sashelp.class;
histogram;
run;
Your macro should be something like the following, but then you need to repeat whatever your analysis is for each table. So then you need another macro, which probably leads to another macro and you're down the road to code you can't reuse or understand.
%macro distribution(varlist=);
%let i=1;
%do %while (%scan(&varlist, &i) ^=%str());
%let var=%scan(&varlist, &i);
proc sql;
create table &var as
select var
from monthend.final_201509
;
quit;
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%distribution( varlist=var1 var2 var3);
What do you mean by check the distribution?
Why not something as simple as below. It generates all the summaries for all numeric variables in the dataset by default.
proc univariate data=sashelp.class;
histogram;
run;
Your macro should be something like the following, but then you need to repeat whatever your analysis is for each table. So then you need another macro, which probably leads to another macro and you're down the road to code you can't reuse or understand.
%macro distribution(varlist=);
%let i=1;
%do %while (%scan(&varlist, &i) ^=%str());
%let var=%scan(&varlist, &i);
proc sql;
create table &var as
select var
from monthend.final_201509
;
quit;
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%distribution( varlist=var1 var2 var3);
Thanks Reeza a lot for your help. Your code works great. I am just curious why my code didn't work. My logic looks ok, but ....
The logic is ok, in terms of concepts and pseudocode, but it's not valid SAS synax.
The concept of creating a list and accessing the components one at a time is correct., but there is no concept of an array in macro's, so you can't refer to them as abc(i).
One workaround is to create them as a list and then access one at a time using Scan to parse the list - which is what my solution implements. This is the same as your logic.
Got it. Thanks again for detailed explanation.
Best,
Harry
I suspect that by "didn't work" in this case you meant there were errors. It is very helpful when discussing errors to post results from the LOG including the code and all error messages into an entry box opened by using the "run" icon in the forum menu. The position of some elements of the error messages is important and the entry box will preserve line formatting that the main box of the message loses.
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.
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.