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!
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;
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;
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?
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.