Pass dataset variable value to a macro

Accepted Solution Solved
Reply
New Contributor RXM
New Contributor
Posts: 3
Accepted Solution

Pass dataset variable value to a macro

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

 

 


Accepted Solutions
Solution
‎08-24-2017 12:36 PM
Super User
Posts: 21,464

Re: Pass dataset variable value to a macro

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


All Replies
Super User
Posts: 21,464

Re: Pass dataset variable value to a macro

Please show how you're using this.

 

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

Super User
Posts: 5,963

Re: Pass dataset variable value to a macro

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.

Super User
Posts: 21,464

Re: Pass dataset variable value to a macro

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

New Contributor RXM
New Contributor
Posts: 3

Re: Pass dataset variable value to a macro

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

Solution
‎08-24-2017 12:36 PM
Super User
Posts: 21,464

Re: Pass dataset variable value to a macro

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

 

 

New Contributor RXM
New Contributor
Posts: 3

Re: Pass dataset variable value to a macro

Reeza,

 

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

 

RXM

Super User
Super User
Posts: 7,380

Re: Pass dataset variable value to a macro


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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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