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
@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.
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)
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.