Multiple Do Loop Macro

Solved
Occasional Contributor
Posts: 9

Multiple Do Loop Macro

Having trouble with the below :

---------------------------------------------------------------------------------------------

%MACRO JUNK;

%LET CDNC1 = A ;

%LET CDNC2 = B ;

%LET CDNC3 = C ;

%LET CDNC4 = D ;

%LET CDNC5 = E ;

%LET CDNC6 = F ;

%LET OFF1 = 101101;

%LET OFF2 = 001110;

%LET OFF3 = 011001;

DATA JUNK (DROP =LG AIGP);

LENGTH M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12 M13 M14 LG \$2. AIGP \$1. ;

%DO H=1 %TO 6;

LG = "&&CDNC&H..";

%DO I=1 %TO 3;

%DO E=1 %TO 6;

/*  if SUBSTR("&&OFF&I",&E,1) ='1' then  */   AIGP = SUBSTR("&&OFF&I",&E,1);

M&E = CATT(LG,AIGP);

M%SYSEVALF(&E+6)= CATT(LG,AIGP);

%IF &E LE 2 %THEN %DO;

AIGP = SUBSTR("&&OFF&I",&E,1);

M%SYSEVALF(&E+12)= CATT(LG,AIGP);

%END;

%END;

%END;

OUTPUT;

%END;

OUTPUT;

RUN;

%MEND;

%JUNK;

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Commented out the portion that is now working.

Essentially, I do not want to output anything into the cells when the condition hits = 0.

I have tried changing out the '1' to 1 just in case. Still doesn't work.

Any ideas on what I could be doing wrong here?

Accepted Solutions
Solution
‎01-06-2014 10:34 AM
Super User
Posts: 8,075

Re: Multiple Do Loop Macro

So here is one way to get what it looks like you want. Note that I did not output the extra copy of the last row.

data junk ;

array m \$2 m1-m14 ;

drop condition offset month i ;

do condition = 'A','B','C','D','E','F' ;

do offset = '101101','001110','011001' ;

do month = 1 to dim(m) ;

i = 1 + mod(month,length(offset)) ;

m(month) = catt(condition,tranwrd(substr(offset,i,1),'0',' '));

end;

output;

end;

end;

run;

proc print; run;

All Replies
Super User
Posts: 13,508

Re: Multiple Do Loop Macro

When which condition hits 0?

What is the output supposed to look like?

How do we know it isn't working?

Occasional Contributor
Posts: 9

Re: Multiple Do Loop Macro

My apologies... The desired output is the following. Notice that all the "0"'s have been removed.

 M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12 M13 M14 A A1 A1 A A A1 A A1 A1 A A A1 A A1 B B1 B1 B B B1 B B1 B1 B B B1 B B1 C C1 C1 C C C1 C C1 C1 C C C1 C C1 D D1 D1 D D D1 D D1 D1 D D D1 D D1 E E1 E1 E E E1 E E1 E1 E E E1 E E1 F F1 F1 F F F1 F F1 F1 F F F1 F F1 F F1 F1 F F F1 F F1 F1 F F F1 F F1
Frequent Contributor
Posts: 83

Re: Multiple Do Loop Macro

This was an issue with the quotations and ampersands.

This code will work.

%MACRO JUNK;

%LET CDNC1 = A ;

%LET CDNC2 = B ;

%LET CDNC3 = C ;

%LET CDNC4 = D ;

%LET CDNC5 = E ;

%LET CDNC6 = F ;

%LET OFF1 = 101101;

%LET OFF2 = 001110;

%LET OFF3 = 011001;

DATA JUNK;

LENGTH M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12 M13 M14 \$2.;

%DO H=1 %TO 6;

%let LG = &&CDNC&H..;

%DO I=1 %TO 3;

%DO E=1 %TO 6;

%if %SUBSTR(&&off&i..,&E,1) =1 %then %do;

%let AIGP = %SUBSTR(&&off&i..,&E,1);

M&E = CATT("&LG", "&AIGP");

M%SYSEVALF(&E+6)= CATT("&LG", "&AIGP");

%end;

%else %DO;

%let AIGP = %SUBSTR(&&off&i..,&E,1);

%if %SYSEVALF(&E+12) <=14 %then %do;

M%SYSEVALF(&E+12)= CATT("&LG", "&AIGP");

%end;

%END;

%END;

%END;

OUTPUT;

%END;

OUTPUT;

RUN;

%MEND;

%JUNK;

I created macro variables for LG and AIGP every time because I found that to be easier than having them put into the table, also the way the code was orignially written it would have gone up to M16, which is why there was a conditional statement saying that if it was less than equal to 14 to keep it but otherwise to get rid of it. Also I assumed that the commented code was supposed to be a then do statement, if not then that would have to be adjusted. Hope this is helpful.

Occasional Contributor
Posts: 9

Re: Multiple Do Loop Macro

Thanks Overmar. I think this is step in the right direction. I will take your condition and see if I can change it to fit the desired output.

Super User
Posts: 8,075

Re: Multiple Do Loop Macro

Write the data step using normal DO loops and constant values.  Then when you have it working use the macro code (or macro variables) to replace the constant values.  It is 10,000 times easier to debug a data step than a macro.  Plus the DO statement is much more flexible than the %DO statement.

data junk ;

array m \$2 m1-m16 ;

length lg \$2 offset \$6 aigp \$1;

do lg= 'a','b','c','d','e','f' ;

do offset = '101101','001110','011001' ;

do i=1 to 6 ;

* put your manipulations here ;

end;

end;

end;

run;

Occasional Contributor
Posts: 9

Re: Multiple Do Loop Macro

```Tom wrote:

Write the data step using normal DO loops and constant values.  Then when you have it working use the macro code (or macro variables) to replace the constant values.  It is 10,000 times easier to debug a data step than a macro.  Plus the DO statement is much more flexible than the %DO statement.

data junk ;
array m \$2 m1-m16 ;
length lg \$2 offset \$6 aigp \$1;
do lg= 'a','b','c','d','e','f' ;
do offset = '101101','001110','011001' ;
do i=1 to 6 ;
* put your manipulations here ;
end;
end;
end;
run;
```

Trying to... This was passed off to me (normally follow as SAS teaches) and I am not a very good expert when there is more than one iterative do loop.

In this case there are 3 - 1 for the conditions, one for the binary strings, and then another again to fill the conditions by the 14 months columns.

Solution
‎01-06-2014 10:34 AM
Super User
Posts: 8,075

Re: Multiple Do Loop Macro

So here is one way to get what it looks like you want. Note that I did not output the extra copy of the last row.

data junk ;

array m \$2 m1-m14 ;

drop condition offset month i ;

do condition = 'A','B','C','D','E','F' ;

do offset = '101101','001110','011001' ;

do month = 1 to dim(m) ;

i = 1 + mod(month,length(offset)) ;

m(month) = catt(condition,tranwrd(substr(offset,i,1),'0',' '));

end;

output;

end;

end;

run;

proc print; run;

🔒 This topic is solved and locked.