I've a macro variable for which the format may be either character or numeric. I want it to be(convert it if necessary) in numeric. How to check whether the variable is character or numeric? I used anyalpha and anydigit functions. The problem is it checks on the name of the variable resolved by the macro variable and not on the value of the variable.
SAS Macro variables are -ALWAYS- character strings. SAS dataset variables are either character or numeric. Because a SAS macro variable is a character string, you can use it as needed in a SAS program. For example:
%let amt = 500;
title "Report on Sales GT &amt";
where sales gt &amt;
In the above example, the macro variable, &AMT is a character string. In the TITLE statement, the character string resolves to a title statement of:
[pre] Report on Sales GT 500[/pre]
The TITLE statement and the string "500" are informative and descriptive. However in the WHERE statement, the 500 goes to the compiler as a numeric constant (as if you had TYPED 500 instead of &AMT).
So when you say the format of a macro variable is either character or numeric, you might mean that you need to use your macro variable as part of a character string or as a numeric constant. However, you go on to explain that the macro variable seems to hold the name of a SAS dataset variable????
I find this somewhat confusing and seeing the code you're using might be helpful here. Knowing a bit more about your data and your goal (what you're trying to accomplish) would also be useful.
The program below creates some fake data -- with 2 character variables C_AGE and C_HEIGHT. Then the type of those 2 variables is checked and new variables are created based on the result of using VTYPE.
** all variables in WORK.NEWCLASS are character except for WEIGHT var;
** need some fake data to work with;
data newclass(keep=name c_age c_height weight);
length c_age c_height $8;
c_age = put(age,2.0);
c_height = put(height,4.1);
** use VTYPE function;
if vtype(c_age) = 'C' then do;
newage = input(c_age,best8.);
if vtype(c_height) = 'C' then do;
newheight = input(c_height,best8.);
if vtype(weight) = 'N' then do;
putlog "Weight Variable is already numeric";
** show results of conversion;
proc print data=whattype;
title 'Creating numeric variables, if needed';
I getting the impression you have some misunderstood concepts, and some how, I'm getting the feeling you may not need a macro variable for your particular problem.
Could you please share your code?
Anyway. You cannot apply directly a function over a macro variable name.
Either resolve the macro to it's value at run time (ampersand notation) or at compile time using the symget function.
anyalpha("&MYMACRO") or anyalpha(symget('MYMACRO'))
If your macro holds the name of dataset variable (instead of it's value), then it's the same thing, BUT with one level nesting.
anyalpha("&&&MYMACRO") or anyalpha(symget(symget('MYMACRO')))
To check if an alpha to num conversion is possible I kinda prefer using the input function with the ?? options.
If the conversion cannot be made, the result should be missing (.)