DATA Step, Macro, Functions and more

%let from string with multiple values separated by +

Reply
Regular Contributor
Posts: 199

%let from string with multiple values separated by +

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

Super User
Super User
Posts: 9,840

Re: %let from string with multiple values separated by +

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

 

Frequent Contributor
Posts: 112

Re: %let from string with multiple values separated by +

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. 

Super User
Posts: 10,569

Re: %let from string with multiple values separated by +

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 40

Re: %let from string with multiple values separated by +

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.

Super User
Posts: 24,000

Re: %let from string with multiple values separated by +

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

Ask a Question
Discussion stats
  • 5 replies
  • 160 views
  • 0 likes
  • 6 in conversation