BookmarkSubscribeRSS Feed
Sandhya
Fluorite | Level 6
Hi,

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.

Thanks in advance.

Sandy.
3 REPLIES 3
Cynthia_sas
SAS Super FREQ
Hi:
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:
[pre]
%let amt = 500;
title "Report on Sales GT &amt";
where sales gt &amt;
[/pre]

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.

cynthia
Cynthia_sas
SAS Super FREQ
I had an additional thought about this. You may not need macro variables in order to check the type of a DATASET variable. For example, the VTYPE and VTYPEX functions will return the type of a variable -- they each work slightly differently. They are documented here:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000245993.htm
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000245994.htm

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.

cynthia
[pre]

** 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;
set sashelp.class;
c_age = put(age,2.0);
c_height = put(height,4.1);
run;

** use VTYPE function;
data whattype;
set work.newclass;
if vtype(c_age) = 'C' then do;
newage = input(c_age,best8.);
end;
if vtype(c_height) = 'C' then do;
newheight = input(c_height,best8.);
end;
if vtype(weight) = 'N' then do;
putlog "Weight Variable is already numeric";
end;
run;

** show results of conversion;
ods listing;
proc print data=whattype;
title 'Creating numeric variables, if needed';
run;
[/pre]
DanielSantos
Barite | Level 11
Hi Sandhya.

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.
[pre]
anyalpha("&MYMACRO") or anyalpha(symget('MYMACRO'))
[/pre]
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.
[pre]
anyalpha("&&&MYMACRO") or anyalpha(symget(symget('MYMACRO')))
[/pre]

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

http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000180357.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1589 views
  • 0 likes
  • 3 in conversation