BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LeeSeongWoo_
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
gamotte
Rhodochrosite | Level 12
Hello,

%do j=i+1 to %to 7 ;

=> %do j=&i.+1 %to 7;
LeeSeongWoo_
Fluorite | Level 6

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

gamotte
Rhodochrosite | Level 12

to %to 😉

Kurt_Bremser
Super User

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;
Kurt_Bremser
Super User

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)

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
  

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1336 views
  • 4 likes
  • 4 in conversation