Hello
Please see this program.
Why %put &Mon3.; is not recognized???
%let vector=1804+1803+1712;
%let k=3;
%put &vector.;
%macro Macro0;
%DO i=1 %TO &k.;
%let mon&i.=%scan(&vector.,&i.,+);
%end;
%mend;
%Macro0;
%put &Mon1.;
%put &Mon2.;
%put &Mon3.;
I am sure this has already been mentioned to you before,
Use the code window to present code, it is the {i} above the post area.
Macro is not a replacement for Base SAS, it is a limited text find/replace. Do data processing in Base SAS!!
You code fails to work for any of the macro variables, the reason being is that the macro variables created inside the macro are local to the macro, therefore do not exist outside the macro - another really good reason to not be doing data processing in macro!! If I had to write it, I would do:
%let vector=1804+1803+1712; data _null_; do i=1 to countw("&vector."); call symput(cats('mon',put(i,best.)),scan("&vector.",i,"+")); end; run; %put &Mon1.; %put &Mon2.; %put &Mon3.;
Or you could fix your code:
%let vector=1804+1803+1712; %let k=3; %macro Macro0; %DO i=1 %TO &k.; %global mon&i.; %let mon&i.=%scan(&vector.,&i.,+); %end; %mend; %Macro0; %put &Mon1.; %put &Mon2.; %put &Mon3.;
The macro variables are local to the macro function so they are not resolved outside the macro.
Fix:
%let vector=1804+1803+1712;
%let k=3;
%put &vector.;
%macro Macro0;
%DO i=1 %TO &k.;
%global mon&i.; /*Added*/
%let mon&i.=%scan(&vector.,&i.,+);
%end;
%mend;
%Macro0;
%put &Mon1.;
%put &Mon2.;
%put &Mon3.;
Please let us know if it helped.
Apart from the good and valid suggestions you aready got, I see another issue:
%let vector=1804+1803+1712;
%let k=3;
%macro macro0;
%do i=1 %to &k.;
&k is a redundant value; you will always be forced to change it along with &vector. Miss that change once, and you'll end up with a bug that can be very hard to find. Make your code as much data-driven as you can:
%let vector=1804+1803+1712;
%macro macro0;
%do i=1 %to %sysfunc(countw(&vector,+));
There is a ballot idea by me that asks for the creation of a %countw() macro function to complement the existing %scan(). This would remove the need for %sysfunc here.
Hi,
Try this.
%let vector=1804+1803+1712;
%let k=3;
%put &vector.;
options symbolgen mprint mlogic;
%macro Macro0;
%DO i=1 %TO &k;
%let mon&i=%scan(&vector,&i);
%end;
%put &mon1;
%put &mon2;
%put &mon3;
%mend;
%macro0;
Thanks,
Srianth.
@Ronein Can you please go through and mark the questions that you've posted with the correct answers. Volunteers have taken the time to provide answers, acknowledging the responses is polite and ensures that people will still be willing to help you in the future.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.