BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Myurathan
Quartz | Level 8

Hi Folks,

 

I have following code, 

%macro test (num,country);
	%do i = 1 %to 3;
proc sql outobs=#
create table combined as
select distinct contractID, sourcesystemID, countryid, stage
from ifrs._09_abc
where countryid = &country. and stage = &i;
%end; %mend; %test(10,1);

When it ran it produces the table with only stage 3. I think the loop replaces the values. I want to have all three stages in the output table. 

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Then you need to append the proc sql output to a dataset each loop, i.e.

 

data want;
  stop;
run;

%macro test (num,country);
  %do i = 1 %to 3;
    proc sql outobs=#
      create table combined as
      select distinct contractID, 
                      sourcesystemID, 
                      countryid, 
                      stage
      from            ifrs._09_abc
       where          countryid =&country. 
             and      stage=&i;
    quit;
    data want;
      set want combined;
    run;
  %end;
%mend;

%test(10,1);

So each iteration the output combined is added to want.

 

That being said, I don't see any point for this code at all, simply:

 

%let country=1;
%let stage=3; proc sort data=ifrs._09_abc out=want (keep=contractid...) nodupkey;
by contractid...;
where countryid=&country. and stage <= &stage.;
run;

Note add your other variables in where the ... is.

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

You always use the same name for the output table, so the second iteration overwrites the result from the first, and the third the second.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Then you need to append the proc sql output to a dataset each loop, i.e.

 

data want;
  stop;
run;

%macro test (num,country);
  %do i = 1 %to 3;
    proc sql outobs=&num;
      create table combined as
      select distinct contractID, 
                      sourcesystemID, 
                      countryid, 
                      stage
      from            ifrs._09_abc
       where          countryid =&country. 
             and      stage=&i;
    quit;
    data want;
      set want combined;
    run;
  %end;
%mend;

%test(10,1);

So each iteration the output combined is added to want.

 

That being said, I don't see any point for this code at all, simply:

 

%let country=1;
%let stage=3; proc sort data=ifrs._09_abc out=want (keep=contractid...) nodupkey;
by contractid...;
where countryid=&country. and stage <= &stage.;
run;

Note add your other variables in where the ... is.

 

Myurathan
Quartz | Level 8

Thanks RW9. 

 

I need this code as I want 10 rows of output for each stages. So each country will have 30 rows of data.

 

MadhuKorni
Quartz | Level 8

 

In the first iteration  you are creating a table Combined for Stage1 data.

In the next iteration you are creating a table for stage2 data with the same table name Combined which overrides the first table.

The third iteration creates a table Combined which overrides the table Combined which has Stage2 data.

So you are left with Stage3 data.

 

If you want to create 3 tables for 3 values you can use 
create table Combined&i as ...... and append the tables

 

 


%macro test (num,country);
%do i = 1 %to 3;

proc sql outobs=&num;
create table combined&i as
select distinct contractID, sourcesystemID, countryid, stage
from ifrs._09_abc
where countryid = &country. and stage = &i;
quit;

proc append base= combined data = combined&i;run;

%end;

%mend;

%test(10,1);

 

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
  • 4 replies
  • 1658 views
  • 2 likes
  • 4 in conversation