BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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.;

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.;

 

Satish_Parida
Lapis Lazuli | Level 10

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. 

Kurt_Bremser
Super User

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.

srinath3111
Quartz | Level 8

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.

Reeza
Super User

@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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3991 views
  • 5 likes
  • 6 in conversation