BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bal23
Lapis Lazuli | Level 10

I want to merge two datasets. I previously needed to sort them first, them

data want

set have1 have2

indsname=ind

contributing_tbl=ind;

run;

 

In this way, I get a new variable indicating where it is originally from

 

However, use "set" is too slow and needs to sort first, which takes more time, I want to change to use proc sql;

however, I need advice from you how to have a simiar statement that adds a column that indicting where it is orginally from have1, or have2,

would you please advice? Thanks.

1 ACCEPTED SOLUTION

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

As mentioned by @Doc_Duke, you are not sorting the data nor merging it.  The set statement in the given example will take all the data from table1, then below that will set all the data from table2.  You can use the proc append procedure to achieve the same thing (and it would be quicker):

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000070936.htm

 

But if your fixed on SQL, you won't get much speed gains, you would use union command - note columns have to all be the same:

 

proc sql;
  create table WANT as
  select   *
  from     (select *,"From Table1" as INTBL from TABLE1) 
  union all
  select   *
  from     (select *,"From Table2" as INTBL from TABLE2);
quit;

 

 

View solution in original post

5 REPLIES 5
Doc_Duke
Rhodochrosite | Level 12

The syntax you have is not technically correct (missing multiple semi-colons), so it is hard to understand what you are really trying to do.  The DATA step, as stated, does not require the data sets to be sorted; is there a BY statement missing?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

As mentioned by @Doc_Duke, you are not sorting the data nor merging it.  The set statement in the given example will take all the data from table1, then below that will set all the data from table2.  You can use the proc append procedure to achieve the same thing (and it would be quicker):

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000070936.htm

 

But if your fixed on SQL, you won't get much speed gains, you would use union command - note columns have to all be the same:

 

proc sql;
  create table WANT as
  select   *
  from     (select *,"From Table1" as INTBL from TABLE1) 
  union all
  select   *
  from     (select *,"From Table2" as INTBL from TABLE2);
quit;

 

 

Bal23
Lapis Lazuli | Level 10

Thank you!

Bal23
Lapis Lazuli | Level 10

My actual problem is,

combine first two into class I; then combine the last two into class II; then combine these two classes into one big dataset;

can you give advice how to do it with one proc sql step?

 

Thanks

LinusH
Tourmaline | Level 20
You are confusing merge with append...?
Your code does append data and then there is no (technical) reason to pre sort the data.
And what is slow?
Show a log of the performance, and tell us what your requirement is.

If we ignore your code a talk about merge, yes, in SQL you don't need to pre sort the data. But SQL usually does that for you behind the scenes, and that isn't necessarily faster (unless you have lots of RAM).
Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 1283 views
  • 0 likes
  • 4 in conversation