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

Hello all,
I need to find a way to use loop to union my datasets.
For example, I have three tables with different column names. I want to union them all by using "outer nuion corr".
The code like this:

proc sql;
create table new as
select * from a1
outer union corr
select * from b1
outer union corr
select * from c1 ;
quit;

I want to use the loop function to do these unions.

%macro union_a;
proc sql;
create table new as
select * from a1
%do i = 2 %to 3;
outer union corr select * from a&i
;
quit;

%end;
%mend;
%union_a;

it gives me an error:

 

NOTE: Line generated by the invoked macro "UNION_A".
5 outer union corr select * from a&i ;
-----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

How should I do this? Thank you!

JH

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
3 REPLIES 3
Tom
Super User Tom
Super User

Your %END is in the wrong place.

jhusoc
Obsidian | Level 7
Oh yes! Thank you!
Reeza
Super User

SET works much easier and better here. 

 

data want;
set a1 b1 c1;
run;

@jhusoc wrote:

Hello all,
I need to find a way to use loop to union my datasets.
For example, I have three tables with different column names. I want to union them all by using "outer nuion corr".
The code like this:

proc sql;
create table new as
select * from a1
outer union corr
select * from b1
outer union corr
select * from c1 ;
quit;

I want to use the loop function to do these unions.

%macro union_a;
proc sql;
create table new as
select * from a1
%do i = 2 %to 3;
outer union corr select * from a&i
;
quit;

%end;
%mend;
%union_a;

it gives me an error:

 

NOTE: Line generated by the invoked macro "UNION_A".
5 outer union corr select * from a&i ;
-----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

How should I do this? Thank you!

JH

 

 


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1004 views
  • 1 like
  • 3 in conversation