BookmarkSubscribeRSS Feed
librasonali
Quartz | Level 8

Given the SAS program shown below:

%let category=Upper;
data work.totals;    set libr.input;    if range eq <enter text here> then amount=4000;    else amount= 2000; run; 

In the space below, the code required to reference the macro variable defined in the program.

please give me the solution with little explanation . thanks in advance. 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Assuming range is a character variable, you want

 

if range eq "&category" then amount=4000;

Explanation: I had to guess, the problem isn't completely clear to me, maybe the above doesn't work. Maybe I need more information about the problem. If that works, then that's how you would refer to a macro variable &CATEGORY so you can test if a data step character variable has the same value as the macro variable, as you have to put the value in single or double quotes to compare it to a data step character variable; and single quotes can't be used with a macro variable inside, because then the macro variable won't resolve, so it has to be double quotes.

 

--
Paige Miller
PaigeMiller
Diamond | Level 26

If the above code works, then I gave a pretty weak explanation.

 

To have any comparison work in a DATA step, without macro variables you need something like this

 

if range eq "Upper" then amount=4000;

This assumes range is a character variable and you want to test to see if its value is "Upper". Single quotes work here too. This is an important first step, getting code to work in one situation without using macro and without macro variables. If you don't have this working, then STOP RIGHT THERE and get this to work. If you don't have this working, then anything you do with macro will not work either.

 

 

Now that you have that working, to use the macro variable, you must use double-quotes and you place the macro variable inside the double quotes

 

if range eq "&category" then amount=4000;

 

 

 

 

--
Paige Miller
Reeza
Super User

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1403 views
  • 1 like
  • 3 in conversation