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

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

1 ACCEPTED SOLUTION

Accepted Solutions
heffo
Pyrite | Level 9

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);

View solution in original post

21 REPLIES 21
Reeza
Super User

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


 

Ian_Tfirn
Calcite | Level 5

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 ();

Reeza
Super User
Ok, I've posted a working version of your attempt above, and a much cleaner one above that. I doubt it's super efficient but it's written and works 🙂
Ian_Tfirn
Calcite | Level 5

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.

Reeza
Super User

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.


 

Reeza
Super User
%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

heffo
Pyrite | Level 9

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);

Reeza
Super User
Post the full code you ran.
Ian_Tfirn
Calcite | Level 5

 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.

Reeza
Super User
I forgot type returns num/char not N/C but that's an easy fix if you compare the two.
Ian_Tfirn
Calcite | Level 5

Even when I change N to num, it still runs proc freq for my numeric var

Ian_Tfirn
Calcite | Level 5

Works perfect now. Thanks for all your help!

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 21 replies
  • 2335 views
  • 6 likes
  • 6 in conversation