hello . I would like to generate several data set by using macro and do loop. but the log message keep alarming .
I don't know why eval is needed .
data work.aa;
input a ;
cards;
1
2
3
4
5
6
7
8
9
10
;
run;
%macro do_loop ;
%do i=1 %to 5 ;
%do j=i+1 to %to 7 ;
data date&i.&j. ;
set work.aa;
if 0<=a<&i then do ;
own1=1 ;
own2= 0 ;
end;
else if &i<= a =<&j then
do;
own1 = 0 ;
own2 = 0 ;
end;
else if &j < a <10 then do ;
own1 = 0 ;
own2 = 1 ;
end;
run;
proc print; run;
%end ;
%end;
%mend ;
%do_loop
To use a macro variable, you always need the ampersand, and in macro %do statements, the "to" must also have a macro trigger:
%do i = 1 %to 5;
%do j = &i+1 %to 7;
Thank you for your reply . I missed it . but after fix it . error does not disappear.
%macro do_loop ;
%do i=1 %to 5 ;
%do j=&i+1 to %to 7 ;
data date&i.&j. ;
set work.aa;
if 0<=a<&i then do ;
own1=1 ;
own2= 0 ;
end;
else if &i<= a =<&j then
do;
own1 = 0 ;
own2 = 0 ;
end;
else if &j < a <10 then do ;
own1 = 0 ;
own2 = 1 ;
end;
run;
proc print; run;
%end ;
%end;
%mend ;
%do_loop
to %to 😉
To use a macro variable, you always need the ampersand, and in macro %do statements, the "to" must also have a macro trigger:
%do i = 1 %to 5;
%do j = &i+1 %to 7;
On top of everything else, compare this with your original code:
%macro do_loop;
%do i = 1 %to 5;
%do j = &i + 1 %to 7;
data date&i.&j.;
set work.aa;
if 0 <= a < &i
then do;
own1 = 1;
own2 = 0;
end;
else if &i <= a =< &j
then do;
own1 = 0;
own2 = 0;
end;
else if &j < a < 10
then do;
own1 = 0;
own2 = 1;
end;
run;
proc print;
run;
%end;
%end;
%mend;
and decide which is more readable and easier to maintain.
(consistent spacing and indentation)
To be honest, I am looking at that code and wondering what process could posible warrant a) 5 datasets with essentially the same data, and b) that code to get to it. Makes no sense at all. The essence of by group processing is to add groups within the existing data, then use those rather than creating lots of copies of data, then having to code loops over it etc.
data want; set have; <series of if statements> <assign group value> run; proc print data=want; by group_value; run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.