BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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.

sas-innovate-2024.png

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.

 

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.

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
  • 5 replies
  • 3076 views
  • 5 likes
  • 6 in conversation