BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

Hello,

 

How can I refer a macro variable and its value in a data step? 

for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:

 

if &colvar.=A1 and &colvar. in (1,2) then do;...

how to refer a macro variable name, and how to refer a value that belong to a variable?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

@stataq wrote:

Hello,

 

How can I refer a macro variable and its value in a data step? 

for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:

 

if &colvar.=A1 and &colvar. in (1,2) then do;...

how to refer a macro variable name, and how to refer a value that belong to a variable?


If I understand your question correctly, you simply code:

 

if symget('colvar'='A1') then if &colvar. in (1,2) then do;...

Note that all values of &colvar must exist as variables in the data set since there's code to test them, or SAS wil complain.

Otherwise a better method would be

 

%if &colvar.=A1 %then %do;
if &colvar. in (1,2) then do;...end;
%end;

 Here, only the appropriate test is inserted in the data step.

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

@stataq wrote:

Hello,

 

How can I refer a macro variable and its value in a data step? 

for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:

 

if &colvar.=A1 and &colvar. in (1,2) then do;...

how to refer a macro variable name, and how to refer a value that belong to a variable?


If I understand your question correctly, you simply code:

 

if symget('colvar'='A1') then if &colvar. in (1,2) then do;...

Note that all values of &colvar must exist as variables in the data set since there's code to test them, or SAS wil complain.

Otherwise a better method would be

 

%if &colvar.=A1 %then %do;
if &colvar. in (1,2) then do;...end;
%end;

 Here, only the appropriate test is inserted in the data step.

stataq
Quartz | Level 8
dose that mean "&colvar.=A1" and "&colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?
ChrisNZ
Tourmaline | Level 20

@stataq wrote:
dose that mean "&colvar.=A1" and "&colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?

"&colvar.=A1" and "&colvar. in (1,2)"

are obviously 2 different ways to test since you're testing against different values (A1 and 1,2)

 

The first test would typically be macro language test (where I used %if) to know the value given to the macro variable, while the second test tests a data set variable whose name is &colvar, i.e A1 for example (and if is used) to know the value of the data set variable.

 

PaigeMiller
Diamond | Level 26

@stataq wrote:

 

How can I refer a macro variable and its value in a data step? 

for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:

 

if &colvar.=A1 and &colvar. in (1,2) then do;...

how to refer a macro variable name, and how to refer a value that belong to a variable?


Please remember that when you run your SAS code, macro variables are replaced by their values. So when you run the code above, it actually becomes

 

if A1=A1 and A1 in (1,2) then do; ...

which doesn't seem like a sensible thing to do, because A1 is always equal to A1. It sounds like you want, if I am understanding you correctly, to test to see if macro variable &colvar is equal to A1 and then test if data set variable A1 is in (1,2). If that's correct then the second bit of code from @ChrisNZ is what you want.

 

dose that mean "&colvar.=A1" and "&colvar. in (1,2)" are correct way to refer a macro variable name and value, but they should not be used in the same step using and to connect the conditions?

 

Please note that it seems like you are using %IF and IF interchangeably in your code and in your thinking. They are not interchangeable. %IF will only test value of macro variables (%IF cannot test values of data set variables) and IF tests values of data set variables or constants.

--
Paige Miller
Tom
Super User Tom
Super User

@stataq wrote:

Hello,

 

How can I refer a macro variable and its value in a data step? 

for example, if I have a dataset that have 3 variables A1 B2 and C3. If I set marco variable "%letr colvar=A1"., how can I utilized &colvar. to get a filter that macro variable colvar was setup as 'A1" and its value is within (1,2)?:

 

if &colvar.=A1 and &colvar. in (1,2) then do;...

how to refer a macro variable name, and how to refer a value that belong to a variable?


FILTER to do what?  

What SAS code do you want to run when COLVAR is A1?

What SAS code do you want to run when COLVAR is something else?

 

If by filter you mean subset then perhaps what you want to do is conditionally generate a WHERE statement?

data want;
  set have;
%if %qupcase(&colvar)=A1 %then %do;
  where A1 in (1,2) ;
%end;
run;

So that WANT is either a full copy of HAVE or a subset of HAVE with just the observations that have a value of A1 that is either 1 or 2.

 

If you want to test the value of a macro variable in SAS code then you can either convert the value to a string literal by enclosing it quotes.

if upcase("&colvar")='A1' then ....

Or use the SYMGET() function to retrieve its value (which avoids issues when the value contains double quote characters.

if upcase(symget('colvar'))='A1' then ....
Quentin
Super User

Can you post a small example, showing what you're trying to do.  Just a dataset with one or two variables and five rows would suffice. 

 

And also show the DATA step code you want to generate, and the macro language code.  If you're building a macro, you want to start by writing the data step code without macro language elements, then you can use the macro language to generate that data step code.

 

 

 

 

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 6 replies
  • 1831 views
  • 3 likes
  • 5 in conversation