BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RXM
Fluorite | Level 6 RXM
Fluorite | Level 6

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

 

View solution in original post

7 REPLIES 7
Reeza
Super User

Please show how you're using this.

 

An alternative - a better one IMO - is to use a FORMAT. 

Astounding
PROC Star

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.

Reeza
Super User

%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?

RXM
Fluorite | Level 6 RXM
Fluorite | Level 6

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

Reeza
Super User

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

 

 

RXM
Fluorite | Level 6 RXM
Fluorite | Level 6

Reeza,

 

Thank you so much!    I knew there would be an elegant solution!

 

RXM

Tom
Super User Tom
Super User

@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: 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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3035 views
  • 2 likes
  • 4 in conversation