Hello all,
As the subject says, I'd like to create a macro that does the following:
1. Identifies a specific variable
2. determines if that variable is character or numeric
3. If it's character, runs proc freq. If it's numeric, runs proc means
I've been attempting various macros that do this but none of have been remotely successful. Any ideas are greatly appreciated.
Thanks
I like to use sashelp.vcolumn! 🙂
%macro doAnalysis(libname,tablename,variable);
%macro dummy(); %mend dummy; *Only here to fix syntax highligthing! ;
proc sql noprint;
select type into : type
from sashelp.vcolumn
where libname = upcase("&libname") and
memname = upcase("&tablename") and
upcase(name) = upcase("&variable");
quit;
%let type =&type; *Get rid of extra spaces;
%if "&type" = "char" %then %do;
proc freq data=&libname..&tablename;
table &variable;
run;
%end;
%if "&type" = "num" %then %do;
proc means data=&libname..&tablename;
var &variable;
run;
%end;
%mend doAnalysis;
%doAnalysis(sashelp, class, sex);
%doAnalysis(sashelp, class, age);
Post what you've tried so far please.
This macro does some of that, except you need to specify what is continuous and what is categorical and binary. The reason I designed it this way is that a numeric variable could still be categorical so using user defined rules seems easier.
https://gist.github.com/statgeek/2f27939fd72d1dd7d8c8669cd39d7e67
@Ian_Tfirn wrote:
Hello all,
As the subject says, I'd like to create a macro that does the following:
1. Identifies a specific variable
2. determines if that variable is character or numeric
3. If it's character, runs proc freq. If it's numeric, runs proc means
I've been attempting various macros that do this but none of have been remotely successful. Any ideas are greatly appreciated.
Thanks
The general idea I've tried is the following:
proc contents data = original output out = new; run;
%macro Freq_means (var = BMI);
data new;
set new;
if Name = "&Var" and Type = 1 then do;
proc means data = original;
var &var;
run;
If Name = "&Var" and Type = 2 then do;
proc freq data = original;
tables &var;
run;
%mend;
%Freq_means ();
Hey Reeza,
Unfortunately, your most recent suggestion will run proc means on all numeric variables and proc freq on all character variables. In datasets with hundreds of variables, that's a lot of results!
I only want the macro to identify a single variable, determine if it is character or numeric, and then run the appropriate procedure.
1. SASHELP.VCOLUMN has all variable types, use PROC SQL to get variable types
2. Write macro conditions to execute as necessary.
There are various ways to do #1, proc contents is another, using functions is another option.
%macro analyze_custom(libname=, dset=, var=); /***insert step 1****/ proc sql noprint; select type into :vtype where libname = upcase("&libname") and memname = upcase("&dset") and upcase(name) = upcase("&var"); quit; %if vtype = N %then %do; /*Insert proc means*/ %end; %else %do; /*insert proc freq*/ %end; %mend; %analyze_custom(libname=sashelp, dset=class, var=sex); %analyze_custom(libname=sashelp, dset=class, var=age);
@Ian_Tfirn wrote:
Hey Reeza,
Unfortunately, your most recent suggestion will run proc means on all numeric variables and proc freq on all character variables. In datasets with hundreds of variables, that's a lot of results!
I only want the macro to identify a single variable, determine if it is character or numeric, and then run the appropriate procedure.
%macro analyze(dataset);
proc means data=&dataset;
var _numeric_;
run;
proc freq data=&dataset;
table _character_;
run;
%mend;
%analyze(sashelp.class);
And that would be a very basic approach to this as well.
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
I like to use sashelp.vcolumn! 🙂
%macro doAnalysis(libname,tablename,variable);
%macro dummy(); %mend dummy; *Only here to fix syntax highligthing! ;
proc sql noprint;
select type into : type
from sashelp.vcolumn
where libname = upcase("&libname") and
memname = upcase("&tablename") and
upcase(name) = upcase("&variable");
quit;
%let type =&type; *Get rid of extra spaces;
%if "&type" = "char" %then %do;
proc freq data=&libname..&tablename;
table &variable;
run;
%end;
%if "&type" = "num" %then %do;
proc means data=&libname..&tablename;
var &variable;
run;
%end;
%mend doAnalysis;
%doAnalysis(sashelp, class, sex);
%doAnalysis(sashelp, class, age);
Hey Reeza,
For some reason, the macro in your most recent comment wasn't working for me while Heffo's did. Since they are nearly identical, I'm sure I was doing something wrong.
Anyway, thanks for your patience. I appreciate you sticking with me.
Even when I change N to num, it still runs proc freq for my numeric var
Works perfect now. Thanks for all your help!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.