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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

7 REPLIES 7
ballardw
Super User

When which condition hits 0?

What is the output supposed to look like?

How do we know it isn't working?

Sasnoob
Calcite | Level 5

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

M1M2M3M4M5M6M7M8M9M10M11M12M13M14
AA1A1AAA1AA1A1AAA1AA1
BB1B1BBB1BB1B1BBB1BB1
CC1C1CCC1CC1C1CCC1CC1
DD1D1DDD1DD1D1DDD1DD1
EE1E1EEE1EE1E1EEE1EE1
FF1F1FFF1FF1F1FFF1FF1
FF1F1FFF1FF1F1FFF1FF1
overmar
Obsidian | Level 7

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.

Sasnoob
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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;

Sasnoob
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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;

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
  • 7 replies
  • 8842 views
  • 6 likes
  • 4 in conversation