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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.