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

Hello,

 

I was wondering if someone could explain this loop for me please, and if it's well indented and stuff.

put "loop start";
     %do j=1 %to 9 ;
          %let k=%eval(&j-1);
          put "boucle j=&j, k=&k";       
          if surv&k>0 and surv&j=0 
          then do;
                surv&j=datesurv; 
                put "loop end";
          end;
          else do;
     %end;
     %do j=1 %to 9 ;
          end;
     %end;

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@polpel wrote:

The person who wrote that code explained it to me and it actually makes a lot of sense.

He cleverly used macro language to actually write SAS code which is how you should be using macro language.


Yes, that's what one uses SAS macro language mostly for: To dynamically create SAS code. BUT: The rule of thumb is to only use macro language if you can't do it with normal SAS language - and this is not the case here. 

Compare the macro code you've posted with the Base SAS language alternative @Kurt_Bremser proposes. What do you think is easier to understand, maintain and debug?

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

You want style feedback?

The first PUT statement is wrong.  The generated SAS code does not have any loops.  It is just 9 nested IF/THEN statements.

The other PUT statements also don't make much sense.  Are you trying to see which IF statements are being evaluated for every observation?  Why not just store that information into another variable instead?

 

If you want functionality feedback provide explanation of what the code is trying to do and provide input data and expected output.

Kurt_Bremser
Super User

Almost classic abuse of the macro language for data manipulation. The poor creature who came up with this should have all memory of macros removed from their brains, so that there will be enough space to learn how to use Base SAS properly:

array surv[0:9] surv0-surv9;
do j = 1 to 9;
  if surv[j-1] > 0 and surv[j] = 0
  then do;
    surv[j] = datesurv;
    j=10;
  end;
end;
polpel
Fluorite | Level 6

The person who wrote that code explained it to me and it actually makes a lot of sense.

He cleverly used macro language to actually write SAS code which is how you should be using macro language.

Patrick
Opal | Level 21

@polpel wrote:

The person who wrote that code explained it to me and it actually makes a lot of sense.

He cleverly used macro language to actually write SAS code which is how you should be using macro language.


Yes, that's what one uses SAS macro language mostly for: To dynamically create SAS code. BUT: The rule of thumb is to only use macro language if you can't do it with normal SAS language - and this is not the case here. 

Compare the macro code you've posted with the Base SAS language alternative @Kurt_Bremser proposes. What do you think is easier to understand, maintain and debug?

polpel
Fluorite | Level 6

Okay yes I see. Thanks again for all the help and explanations!

mjizzle
Fluorite | Level 6
 

I have two sets of column names in my database such as columnA_1, columnA_2 ... all the way till 17. And another column name like columnB_1, columnB_2... all the way to 9. I want to create a new variable that checks if these columns have same values (character) and how many of them have same values. For Example consider the following table:

 

A_1     A_2      A_3      A_4                   B_1     B_2       B_3    B_4

John   Adam   David   Sam                  Adam  John    Matt    Tom

 

In the above data, I want the code to check all the columns from A_X with values of columns in B_X and give me a number = 2. i.e Adam and John is common between them. This is the code I am trying but is not working at all. I am not an expert in SAS so I am learning by doing but am unsuccessful so far. Any help is appreciated. 

 

Image 11-20-19 at 11.39 AM.jpg
%macro create(adv);
data nestedcommon2;
set nestedcommon1;
%do i=1 %to 17;
%do j= 1 %to 9;
common=0;
%if acq_adv_&i=tar_adv_&j %then common =  common+1;
%end;
%end;
 final=common;
run;
%mend create;
%create(153)

 

 

Thanks,

Mjizzle

PaigeMiller
Diamond | Level 26

@mjizzle wrote:
 

I have two sets of column names in my database such as columnA_1, columnA_2 ... all the way till 17. And another column name like columnB_1, columnB_2... all the way to 9. I want to create a new variable that checks if these columns have same values (character) and how many of them have same values. For Example consider the following table:

 

A_1     A_2      A_3      A_4                   B_1     B_2       B_3    B_4

John   Adam   David   Sam                  Adam  John    Matt    Tom

 

In the above data, I want the code to check all the columns from A_X with values of columns in B_X and give me a number = 2. i.e Adam and John is common between them. This is the code I am trying but is not working at all. I am not an expert in SAS so I am learning by doing but am unsuccessful so far. Any help is appreciated. 

 

Image 11-20-19 at 11.39 AM.jpg
%macro create(adv);
data nestedcommon2;
set nestedcommon1;
%do i=1 %to 17;
%do j= 1 %to 9;
common=0;
%if acq_adv_&i=tar_adv_&j %then common =  common+1;
%end;
%end;
 final=common;
run;
%mend create;
%create(153)

As stated above to the original poster, get rid of macros and macro variables here. They are a completely unnecessary (and in this case ineffective) way to accomplish the goal. Learn to use data steps and the power of data steps.

 

Specifically, use ARRAYs when you are in a data step and want to perform tasks across multiple variables in the same observation.

 

For example:

 

/* Untested Code */

data nestedcommon2;
    set nestedcommon1;
    array acq_adv acq_adv1-acq_adv17;
    array tar_adv tar_adv1-tar_adv9;
    common=0;
    do i=1 to 17;
        do j=1 to 9;
             if acq_adv(i)=tar-adv(j) then common=common+1;
        and;
    end;
run;

 

--
Paige Miller
mjizzle
Fluorite | Level 6

Perfect! Thank you so much for your quick reply.

 

Best,

Mjizzle

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 807 views
  • 6 likes
  • 6 in conversation