- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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...