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

I need help understanding why nwords will not resolve in the following lines of code:

%let drug=EXAMPLEONE, EXAMPLETWO;

%macro test;

Data test;

    drugs="&drug";

    dm = ',';

    mod = 'oq';

    nwords = countw(drugs, dm, mod);

    call symput('nwords',nwords);

    %do i=1 %to &nwords;

    drug&i=strip(scan(drugs, &i, dm, mod));

    %end;

run;

%mend test;

%test;

However this works:

%macro test;

Data _null_;

    drugs="&drug";

    dm = ',';

    mod = 'oq';

    nwords = countw(drugs, dm, mod);

    call symput('nwords',nwords);

run;

data test;

    drugs="&drug";

    dm = ',';

    mod = 'oq';

    nwords = countw(drugs, dm, mod);

    %do i=1 %to &nwords;

    drug&i=strip(scan(drugs, &i, dm, mod));

    %end;

run;

%mend test;

%test;

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are mixing up macro code and datastep code is the reason:

So, your macro test.  When it is called it will expand all the macro code into useable SAS code, then send that to the compiler.  So your call to symput doesn't get actioned until *after* all the macro has been evaluated, hence %do I=1 to &Nwords will result in a problem as nwords is not defined.  What are you attempting to do, as from the code above there is no reason to resort to macro code anyway.

Data test;

    drugs="&drug";  /* Don't know where this comes from? */

    dm = ',';

    mod = 'oq';

     array drug{100} $200.;  /* I am just assuming no more than 100 */

    do i=1 to countw(drugs, dm, mod);

      drug{I}=strip(scan(drugs,i,dm,mod));

    end;

run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You are mixing up macro code and datastep code is the reason:

So, your macro test.  When it is called it will expand all the macro code into useable SAS code, then send that to the compiler.  So your call to symput doesn't get actioned until *after* all the macro has been evaluated, hence %do I=1 to &Nwords will result in a problem as nwords is not defined.  What are you attempting to do, as from the code above there is no reason to resort to macro code anyway.

Data test;

    drugs="&drug";  /* Don't know where this comes from? */

    dm = ',';

    mod = 'oq';

     array drug{100} $200.;  /* I am just assuming no more than 100 */

    do i=1 to countw(drugs, dm, mod);

      drug{I}=strip(scan(drugs,i,dm,mod));

    end;

run;

dtchoi86
Fluorite | Level 6

Thank you so much for your explanation.  It was very informative and helpful.  If it is not too much, could you explain why the following does not work as well:

%macro test

Data test (drop=drugs dm mod nwords);

    drugs="&drug";

    dm = ',';

    mod = 'oq';

    nwords = countw(drugs, dm, mod);

    %do i=1 %to nwords;

    drug&i=strip(scan(drugs, &i, dm, mod));

    %end;

run;

%mend test;

;

%test

Is this because, similar as your explanation for my previous question, there is a mismatch with data step code and macro code?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Thats correct.  In step one your macro parts are expanded/resolved, as at that point nwords has not been created it fails.  So think of it this way, when the macro-preprocessor works it is trying to create a full base SAS program with nothing macro left in.  That code is then passed onto the compiler to be actioned.  So nwords is part of the datastep in the second step, but you are trying to reference it in macro language in step 1.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 3 replies
  • 784 views
  • 2 likes
  • 2 in conversation