DATA Step, Macro, Functions and more

A Macro to Run Different Macros for Different Data Types

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

A Macro to Run Different Macros for Different Data Types

Hi all,

I'm new to SAS and SAS Macro and I'm having trouble with this. I'm trying to write a macro that will call one macro for categorical data, and another for numerical data. This is what I've tried so far:

 

%macro please_help(dataset, var1);

	%if vartype(&dataset, varnum(&dataset, &var1))=C %then %do;
		%cat_macro(&dataset, &var1);
	%end;
	%else %do;
		%num_macro(&dataset, &var1);
	%end;
%mend;

I'm getting the following error:

ERROR: Required operator not found in expression: vartype(&dataset, varnum(&dataset, &var1))=C

 

Could anyone suggest a correct way to find what type of data is entered, or maybe a different approach? I have also tried vtype and vtypex instead of vartype, and got the same error.

 

Thanks


Accepted Solutions
Solution
‎04-10-2018 07:26 AM
Trusted Advisor
Posts: 1,827

Re: A Macro to Run Different Macros for Different Data Types

[ Edited ]

You cannot use sas functions directly within a macro code but under %sysfunc sas macro function.

Try next code:

%macro please_help(dataset, var1);
        %let dsid = %sysfunc(open(&dataset));
	%if %sysfunc(vartype(&dsid, %sysfunc(varnum(&dsid, &var1)))) = C %then %do;
		%cat_macro(&dataset, &var1);
	%end;
	%else %do;
		%num_macro(&dataset, &var1);
	%end;
%let dsid = %sysfunc(close(&dsid)); %mend;

Alternativly you can try:

%macro my_run  (dataset, var);
       data _null_;
             dsid = open("&dataset");
             if dsid > 0 then do;
                   if vartype(dsid, varnum(dsid,&var)) = 'C'
                   then call execute("%cat_macro(&dataset, &var)" );
                   else call execute("%num_macro(&dataset, &var)" );  
dsid = close(dsid);
end; run; %mend; %my_run;

 

 

View solution in original post


All Replies
Solution
‎04-10-2018 07:26 AM
Trusted Advisor
Posts: 1,827

Re: A Macro to Run Different Macros for Different Data Types

[ Edited ]

You cannot use sas functions directly within a macro code but under %sysfunc sas macro function.

Try next code:

%macro please_help(dataset, var1);
        %let dsid = %sysfunc(open(&dataset));
	%if %sysfunc(vartype(&dsid, %sysfunc(varnum(&dsid, &var1)))) = C %then %do;
		%cat_macro(&dataset, &var1);
	%end;
	%else %do;
		%num_macro(&dataset, &var1);
	%end;
%let dsid = %sysfunc(close(&dsid)); %mend;

Alternativly you can try:

%macro my_run  (dataset, var);
       data _null_;
             dsid = open("&dataset");
             if dsid > 0 then do;
                   if vartype(dsid, varnum(dsid,&var)) = 'C'
                   then call execute("%cat_macro(&dataset, &var)" );
                   else call execute("%num_macro(&dataset, &var)" );  
dsid = close(dsid);
end; run; %mend; %my_run;

 

 

Occasional Contributor
Posts: 11

Re: A Macro to Run Different Macros for Different Data Types

Thanks a million, I tried the first method and it worked perfectly!
Super User
Posts: 9,611

Re: A Macro to Run Different Macros for Different Data Types

Create a dataset with variable attributes from dictionary.columns, and then use call execute from that:

proc sql;
create table control as select libname,memname,name,type from dictionary.columns where libname = upcase("&lib.") and memname = upcase ("&dataset.") and upcase(name) = upcase("&var1.");
quit;

data _null_;
if type = 'char'
then call execute(cats('%cat_macro(',libname,'.',memname,',',name,');'));
else call execute(cats('%num_macro(',libname,'.',memname,',',name,');'));
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 91 views
  • 2 likes
  • 3 in conversation