Hi team,
we need help in sas macro
if we can use below
data test;
do i = 2, 5, 7 ;
x=i;
output;
end;
run;
so what we can not use
%macro x();
%do i=3,6,8;
statements;
%end;
%mend;
%x;
Macro language is NOT meant for data processing. Write this 100 times.
I apology if i did mistake.
But i need to run my programe for selective day. and i want to run my do loop for that.
but i am getting error that u r missing %to. So is there any possibility that i can use %do with out using %to.???
As @Kurt_Bremser has mentioned, Macro language is only present to generate text, it does not do any processing. Therefore constructs used in Base SAS which is the programming language do not always carry through to Macro. Do list of values is one of these constructs.
I believe we have been over this several times before. Do data processing in Base SAS, that is what it is for. What is it your trying to do with this code?
i want to pick selective data by using :
%macro x();
%do i = 3 ,6, 7,9;
proc report data= Ivalue_&i;
run;
%end;
%mend;
data control;
input i;
cards;
3
6
7
9
;
run;
%macro x(i);
proc report data= Ivalue_&i;
run;
%mend;
data _null_;
set control;
call execute('%x(' !! strip(put(i,best.)) !! ') ');
run;
Post the code you want to run for a series of days, showing how the days are represented in the code as is (for a single run), and from where you will get the days for the dynamic execution (dataset?).
When writing repeatable code, follow this sequence:
- write code for one iteration
- identify variable parts
- replace with macro values
- create a means to supply these values (macro %do loop, datastep with call execute, ...) repeatedly
The %DO command does not have all of the functionality of the DO command.
To process a list I usually put the list into a macro variable and use %SCAN() to pull the individual items.
%macro x(list);
%local i value ;
%do i=1 %to %sysfunc(countw(&list));
%let value = %scan(&list,&i);
statements using &value ;
%end;
%mend;
%x(2 5 7);
Thanks for ur answer.
i have done same as alternate option.
%let list=13|14|15|25|26|27;
%do part = 1 %to 6;
%let i=%scan(&list,&part,%str(|));
%put &i;
%end;
but for future prospect i want study this more. bcaz i have tried "in" with %do same also does not work. is there any document so i can study more for the same.
Thanks in advance for all of ur advice.
Note there is no need to use %STR() around | as it is not a macro trigger. That is one good reason to use it as the delimiter in a list of values. Personally I much prefer to use spaces as delimiters since it is much easier to type and read, but that can be a problem if the values can contain spaces. If your values can contain other default delimiters for %SCAN() then you do need to use the third argument to %SCAN() to let it know what specific delimiter(s) to use. There is also a fourth argument to %SCAN() that can be used to add modifiers to control how it parses the string.
The IN operator is just a method for testing if a value appears in a list a values. Not sure what it would have to do with a DO loop.
Note that there are limitations on using the IN operator within macro code since it was not originally recognized as a keyword by the macro processor. There are system options and options on the %MACRO statement to control whether and how the IN operator works in macro code.
The SAS documentation is on-line at sas.com. I usually just GOOGLE until I find the right document and then look around.
If you do want to use %do loop, you could try this:
option minoperator mindelimiter='|';
%macro test;
%let list=13|14|15|25|26|27;
%do part = 1 %to 30;
%if &part in &list %then
%put ∂
%end;
%mend;
%test;
@slchen It's a good habit to declare your variables as local. You'll save yourself many headaches.
So this is a (slightly) better macro:
%macro test/minoperator mindelimiter='|';
%local list part;
%let list=13|14|15|25|26|27;
%do part = 1 %to 30;
%if &part in &list %then %put &=part;
%end;
%mend;
%test;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.