What i am about to ask may seem a little weird but i want to know if it possible::
What i want is :
i) can i assign more than one value to a macro variable in sas?
For eg, % Let Year = 2017, 2018, 2019, 2010, so on...
ii) then i can tell sas that when i use this macro variable "&year" for the first time at any place in my code then pick the first value assigned to it... when i use this macro variable for second time then pick the second value, then third and so on.
Thanks,
First, since the comma is the delimiter in many SAS supplied macro functions, used defined macros and data step functions that may be called a macro with the %sysfunc function it is a poor idea to throw in commas as separators willy-nilly. A space will work just fine.
One way to do what you want with an example of passing the list to macro:
%macro dummy(list= ); %do i = 1 %to %sysfunc(countw(&list.)); %let value = %scan(&list., &i.); %put The &i.th value in list is &value.; %end; %mend; %dummy(list= 1998 2019 1856 abc)
Place a comma in the list to see just one of the headaches caused.
Hey @ballardw actually i'm new to the software and don't know this much level of coding. Can you explain the above code step by step and tell me how can i use this marco.
Thanks in advance.
@hiteshchauhan1 wrote:
Hey @ballardw actually i'm new to the software and don't know this much level of coding. Can you explain the above code step by step and tell me how can i use this marco.
Thanks in advance.
The %PUT statement writes to the LOG exactly what the macro is doing at each step of the loop. That ought to be enough to help you figure out what it happening.
Thanks @PaigeMiller will try that.
@hiteshchauhan1 wrote:
Hey @ballardw actually i'm new to the software and don't know this much level of coding. Can you explain the above code step by step and tell me how can i use this marco.
Thanks in advance.
If SAS is your first entry into programming then generally starting with macros is not the best idea. The SAS macro language is intended to generate program code. If you are not familiar with the basic coding then most new coders aren't sure what basic SAS code is needed and often try to generate "macros" to replace basic features.
For instance one of the very powerful SAS programming feature is "By Group" processing. If you didn't just think to yourself something like "of course" then that is an indication that you may have been about to attempt a macro instead of using BY group processing. By group processing, indicated by use of BY and a list of variables (not values) is supported by almost all procedures as well as the data step. It treats data as belonging together based on the (usually) sorted data values of the variables and generally repeats the processing requested for each level.
Your starting list of apparent sequential years looked like possibly something that could be done with:
By Year;
in some form.
The actual code:
%macro defines the start of a block of macro code, the second is the name of the macro. Inside the () are the parameters that the macro would use, in this case a single parameter named list.
The
%do indicates a block of code that will stop at %end. With the "I = 1" that indicates this will be an iterated loop with values that start at 1. %to indicates what follows should resolve to an integer that indicates how many times we want the loop to run. %sysfunc( ) is a macro function that allows you to call a function normally restricted to use in a data step, in this case the function is Countw which counts, by default, how many space delimited "words" are in a string of values (optionally you can use other characters for delimiter but see the previous comment about commas).
The %scan function pulls out the specified "word" in the position indicated by the loop counting variable &i.
%put writes the result of the assignment to the log.
Once the macro is compiled by submitting the code, assuming no fatal errors in the macro code, the name of the macro preceded by a % to tell SAS it is macro code, followed by the () with the parameters will attempt to execute the statements GENERATED by the code.
In this case the only writes values to the log.
This sort of loop can be used to reference data sets such as
Proc print data=libname.set&value. ;
run;
would print the datasets set1998 set2019 set 856 and setabc in the library named libname. If any of those data sets do not exist you would get an error that the set doesn't exist when attempting to print it.
The example code does no checking such as was the parameter List even assigned any values.
One of the reasons we suggest not attempting macros for awhile is many syntax and some logic errors can render a SAS session somewhat unstable. i.e. appears locked up and submitting additional code will not run. Other reasons are potential of infinite loops creating millions of (possibly empty) data sets and other joys.
If you do go down the %path, save code every time before submitting until you reliable get only exactly what you want from your macros.
Also learn the system options MPRINT SYMBOLGEN MLOGIC.
Thanks @ballardw for the detailed explanation and for those suggestions. I am new to the SAS and yes it is the first programming language i am working on. I will follow what you just told me like strengthening the Basics first and not straight out jump to Macros.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.