Macro - Results not updating

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

Macro - Results not updating

[ Edited ]


data test;
input country$ sales;
cards;
India 1000
US 2000
AMERICA 3000
AMERICA 1000
;run;

 

 


%macro abc();
%do i=1 %to 3;
data _null_;
set test;
call symput('a',scan(country,1));
%put &a;
run;
data &a;
set test;
run;
proc print data=&a;run;
%end;
%mend;
%abc;

 

 

i want to create  a dataset with the country name like 'India','US','AMERICA'.But it creating only dataset with name of 'AMERICA'?

macro variable 'a' is taking only the last country name?

 

 

 


Accepted Solutions
Solution
‎02-13-2016 11:50 PM
Grand Advisor
Posts: 17,378

Re: Macro - Results not updating

1. Create a macro variable that holds the list of countries

ie India USA America

2. Loop through list - many ways. 

 

data test;
input country$ sales;
cards;
India 1000
US 2000
AMERICA 3000
AMERICA 1000
;run;
 
 

%macro abc();

proc sql noprint;
select distinct country 
	into :clist separated by " "
from test;
quit;


%do i=1 %to 3;
%let a=%scan(&clist, &i);

data &a;
set test;
run;

proc print data=&a;
run;

%end;
%mend;
%abc;

View solution in original post


All Replies
Grand Advisor
Posts: 17,378

Re: macro

Every iteration of your loop erases your previous data set, so you're only left with your final results.

Change the macro so that a new data set is obtained for each word.

 

Your problem is similar to the problem here and the solutions here may be what you want:

https://communities.sas.com/t5/General-SAS-Programming/Looking-for-an-elegant-solution/m-p/143764/hi...

Respected Advisor
Posts: 4,989

Re: macro

Yes, you will get just the last country.  Look at this DATA step, a variation of what is in your program:

 

data _null_;
set test;
call symput('a', country);
run;

 

The SET statement reads the first observation (country is India) and CALL SYMPUT assigns India as the value of &a.

 

Then the SET statement reads the second observation (country is US) and CALL SYMPUT replaces &a with US.

 

Then the SET statement reads the third observation (country is AMERIDA) and CALL SYMPUT replaces &a with AMERICA.

 

That's why you only get the last country.

 

There are other conceptual issues to look at.  Trailing blanks within &a.  A %PUT statement in the middle of a DATA step.  A %DO loop that repeats the same actions each time.  Those may arise as you experiment with this going forward.

Contributor
Posts: 59

Re: macro

so how can i get different values(country names) in my macro variable 'a' each time ??
I think i need to concatenate it with _N_,but is there any alternative to that??
Solution
‎02-13-2016 11:50 PM
Grand Advisor
Posts: 17,378

Re: Macro - Results not updating

1. Create a macro variable that holds the list of countries

ie India USA America

2. Loop through list - many ways. 

 

data test;
input country$ sales;
cards;
India 1000
US 2000
AMERICA 3000
AMERICA 1000
;run;
 
 

%macro abc();

proc sql noprint;
select distinct country 
	into :clist separated by " "
from test;
quit;


%do i=1 %to 3;
%let a=%scan(&clist, &i);

data &a;
set test;
run;

proc print data=&a;
run;

%end;
%mend;
%abc;
Respected Advisor
Posts: 4,989

Re: Macro - Results not updating

I know you have a working answer.  I just wanted to point out a possibility based on your original program.  You have:


data _null_;
set test;
call symput('a',scan(country,1));
%put &a;
run;

 

A few small changes will get this to work:

 

data _null_;
set test (obs=&i firstobs=&i);
call symputx('a', country);
run;

%put &a;

 

Note that switching to CALL SYMPUTX will automatically remove trailing blanks.

Occasional Contributor
Posts: 18

Re: Macro - Results not updating

Another way if you do not want to create a list of countries:-

 

%macro abc();


%do i=1 %to 4;
%put &i;

data _null_;
obsnum=input(strip(&i),12.);
set work.test point=obsnum;
call symput ('new',country);
stop;
run;

data &new;
set test;
run;

proc print data=&new;
run;


%end;
%mend;
%abc;

 

Regards,

ABD.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 453 views
  • 3 likes
  • 4 in conversation