I have a simple macro that converts a letter value to a numeric value:
%macro grade(color);
%global colorgrade;
%let incolor=&color;
select(&incolor);
when ('L') colorgrade=8;
when ('B') colorgrade=6;
when ('G') colorgrade=4;
when ('Y') colorgrade=2;
when ('R') colorgrade=0;
otherwise colorgrade=-2;
end;
%mend grade;
My problem is passing the value of the dataset variable to the macro. Instead of the value of the variable ('G'), it passes the variable name (color).
Search Lexjansen.com for any topics, you'll find papers and full code examples:
Page 4 of this paper has an example.
http://www2.sas.com/proceedings/sugi27/p056-27.pdf
Please show how you're using this.
An alternative - a better one IMO - is to use a FORMAT.
From what you have shown so far, the macro actually needs the variable name. It would be wrong to pass the variable value. Certainly this statement would be incorrect syntax in a DATA step:
select ('G');
Secondarily, what is the purpose of the %GLOBAL statement? There is nothing about this code that assigns a value to a macro variable named COLORGRADE.
%global colorgrade; <- what's this for, it's not used in the code you've shown.
%let incolor=&color; <- this isn't needed, renaming the macro variable?
Reeza,
As you can tell, I am a complete neophyte and am converting code fragments from the SAS documentation. I thought of the FORMAT function first, but have not found any examples where it was used to convert a character value to a numeric value.
RXM
Search Lexjansen.com for any topics, you'll find papers and full code examples:
Page 4 of this paper has an example.
http://www2.sas.com/proceedings/sugi27/p056-27.pdf
Reeza,
Thank you so much! I knew there would be an elegant solution!
RXM
@RXM wrote:
Reeza,
As you can tell, I am a complete neophyte and am converting code fragments from the SAS documentation. I thought of the FORMAT function first, but have not found any examples where it was used to convert a character value to a numeric value.
RXM
Formats are for displaying values, so they always generate text. But an INFORMAT can be used to generate numeric values.
proc format ;
invalue colorgrade
'L'=8
'B'=6
'G'=4
'Y'=2
'R'=0
other=-2
;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.