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

I have several data sets and want to merge them.

 

data all7;
length  company $12.;
set demo_1 demo_2 demo_3 demo_4 demo_5 demo_6 demo_7;
run;

The company is the last variable in all the 7 data sets to be merged, but it has different lengths in them.

 

If I use length statement to set them to the same length, the variable becomes the first one in the merged data set.

 

One way to work around is to set the length in all the 7 data sets first before merging. I wonder if there is a way during merge.

thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
First this is an append/set operation not merge (which is similar to a SQL join).

What is the problem with variable order? It's just a physical thing. You usually only need to care about variable order in reports.

But, to answer your question, the only way I see is to declare all variables in a length. Or if you know which data set that has the appropriate length set that as first data set, or set it twice (the first time with obs=0) if you need to have a specific observation order.
Data never sleeps

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20
First this is an append/set operation not merge (which is similar to a SQL join).

What is the problem with variable order? It's just a physical thing. You usually only need to care about variable order in reports.

But, to answer your question, the only way I see is to declare all variables in a length. Or if you know which data set that has the appropriate length set that as first data set, or set it twice (the first time with obs=0) if you need to have a specific observation order.
Data never sleeps
fengyuwuzu
Pyrite | Level 9

you are right. The order of the variables only matters in final report.

I care it only because I have been used to the format, "visual effect" to my self.

 

as for the "merge" term, yes, this is a set operation (concatenation, like union in proc sql), not the speficially used merge term, which is like proc sql join. Thank you for the clarification.

FreelanceReinh
Jade | Level 19

Hello @fengyuwuzu,

 

I think, @LinusH mentioned the solution already: set a dataset twice.

data all7;
if 0 then set demo_1(drop=company); /* creates the PDV without COMPANY */
length company $12;
set demo_1 demo_2 demo_3 demo_4 demo_5 demo_6 demo_7;
run;

The above code assumes that all 7 datasets have the same variables. Otherwise, you could include more datasets into the first SET statement (which is never executed due to the IF condition).

 

Edit: This was also inspired by this recent post from @data_null__, who used this technique to prepare an array definition.

 

fengyuwuzu
Pyrite | Level 9
Thank you so much, FreelanceReinhard.
Your code is so clear. Thanks!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 2388 views
  • 3 likes
  • 3 in conversation