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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

 

 

Sean100
Calcite | Level 5
Thanks a million, I tried the first method and it worked perfectly!
Kurt_Bremser
Super User

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1357 views
  • 2 likes
  • 3 in conversation