BookmarkSubscribeRSS Feed
ertr
Quartz | Level 8

Hello everyone,

 

I try to figure out to recognize macro variables values type and eliminate the character ones. I have two sample as below;

 

In general, there are Vtype option and %Datatyp option to recognize type of variable.

 

For Proc Corr, I tried to use _NUMERIC_ option into Var statement but because data  has more numeric variables than my numeric variables, it does not give my desired output.

 

My first sample as below;

 

/*EXAMPLE1*/

%LET Variables =Num1 Num2 Num3 Char1 Char2 Char3;
%MACRO Correlation(Dataset);

Ods Output SimpleStats=Stats_(Rename=(MEAN=_Mean STDDEV=_StdDev MIN=_Min MAX=_Max MEDIAN=_Median NObs=_NObs));
Proc Corr NoMiss Spearman Data=&Dataset.;
Var &Variables.;
Run; 
Quit;
 
Ods Output SpearmanCorr=SprRaw_;
Proc Corr Spearman Data=&Dataset.;
Var &Variables.;
Run;

%MEND;

%Correlation(WORK.TEST);

And my second example as below;

 

%LET Variables =Num1 Num2 Num3 Char1 Char2 Char3;
%Let CountRaw=%Sysfunc(CountW(&Variables," "));
%Put &CountRaw;

/*EXAMPLE2*/

%MACRO Stab(Dataset);  
%If &CountRaw GT 0 %Then %Do;
     %Do i = 1 %To &CountRaw;
        %Let Var&i = %Scan(&Variables.,&i,%str( ));
        %Put Var&i= &&Var&i;
 
DATA Stab_&Suffix._&i.;
SET &DataSet. (Keep=&&Var&i);
Format &&Var&i 12.2;
Run;

Proc Sort Data=Stab_&Suffix._&i.; By &&Var&i; Run;
 
Data Stab_&Suffix._&i. (Keep=&&Var&i);
Set Stab_&Suffix._&i.;
BinVar=Sum(&&Var&i,(_N_/1000));
Run;
 
%End;
%End;
%MEND;
%Stab(WORK.TEST2);

I would much appreciate, if someone can help me about this subject.

 

Thanks

3 REPLIES 3
Reeza
Super User
Macro variables don't have a type, they are text only. Do you have another rule you can use to filter out the numeric variables you want for analysis? Perhaps based on formats, ie exclude dates and ID?
ballardw
Super User

@ertr wrote:

Hello everyone,

 

I try to figure out to recognize macro variables values type and eliminate the character ones. I have two sample as below;

 

 


MACRO variables are always text.

The %datatype function basically returns numeric if it could be used where a numeric value is expected in code. But the argument is macro variable. If the macro variable contains the name of a data set variable the result will not be numeric because the value of the macro variable would not look like a number in any way.

 

For Proc Corr, I tried to use _NUMERIC_ option into Var statement but because data  has more numeric variables than my numeric variables, it does not give my desired output.

Does not make any sense. _numeric_ cannot return more numeric variables than exist in the data set. Show an example.

 

Proc Corr by default without a VAR statement will include all numeric variables in the analysis.

proc corr data=sashelp.class;
run;

For example provides output for age, height and weight as those are the numeric variables in the data set.

 

 

Please describe what you are actually attempting to do more detail, such as list the variables in your data set, which ones are numeric (proc contents will show that) and which ones you want included.

 

Your second macro looks like you are creating one data set for each variable. That creates such a nightmare I wouldn't go there unless I was getting paid lots of money to use the most inefficient method possible to do something.

PaigeMiller
Diamond | Level 26

I try to figure out to recognize macro variables values type and eliminate the character ones.

 

As others have pointed out, you absolutely have to have clear in your mind that these are data set variables you are going to work with and NOT macro variables. You can work with the data set variables whose names are stored in a macro variable, and then pick out the  data set variables that are actually numeric.

 

The general idea is to let PROC SQL query the dictionary tables to see if a data set variable of one of the given names is type "num" or type "char". The same SQL can then create a new macro variable containing the numeric data set variable names.

 

Something like this:

 


%macro abc1(dataset);
    %let varnames=num1 num2 num3 char1 char2 char3;
    proc sql noprint;
        select name into :varnames2 separated by ' ' from 
             dictionary.columns where libname='WORK' and
             lowcase(memname)="%lowcase(&dataset)" and type='num' and 
             ( 
                    %do i=1 %to %sysfunc(countw(&varnames)); 
                          %let thisvarname=%scan(&varnames,&i);
                           lowcase(name)="%lowcase(&thisvarname)" 
                           %if &i<%sysfunc(countw(&varnames)) %then or; 
                     %end; 
                );
	quit;
	%put &=varnames2;
%mend;
%abc1(yourdatasetname)
--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2756 views
  • 0 likes
  • 4 in conversation