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


Hi

Lets say I have the two following table :

Table : mylib.Test1

NameDateVar1Var2
John2012-01-01aaabbb
Bob2012-01-02eeeccc
Peter2012-01-03ffffddd

Table : Mylib.test2

NameDateVar9
Bob2012-01-04ttt
Fred2012-01-05yyy
Will2012-01-05vvv

I want to merge the two tables, so that I have all the names, dates, and the source table dates (and if name should be repeated, have the multiple entries) like this :

NameDateSource_Table
John2012-01-01test1
Bob2012-01-02test1
Peter2012-01-03test1
Bob2012-01-04test2
Fred2012-01-05test2
Will2012-01-05test2

Thank you very much for your help and time.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

If you don't need var1, var2 and var9 then

data want;

set mylib.test1 (keep=name date) mylib.test2 (keep=name date) indsname=source;

source_table=source;

run;

View solution in original post

6 REPLIES 6
Reeza
Super User

If you don't need var1, var2 and var9 then

data want;

set mylib.test1 (keep=name date) mylib.test2 (keep=name date) indsname=source;

source_table=source;

run;

nicnad
Fluorite | Level 6

Reeza thank you very much for this quick reply.

The set and keep procedure work well.

The only thing is that I am not able to make the part in bold work.

data want;

set mylib.test1 (keep=name date) mylib.test2 (keep=name date) indsname=source;

ERROR 22-322: Syntax error, expecting one of the following: END, KEY, KEYS, NOBS, POINT.

ERROR 76-322: Syntax error, statement will be ignored.

source_table=source;

run;

The error points on the equal sign between indsname=source

Reeza
Super User

That means you have SAS 9.1?

data want;

set mylib.test1 (keep=name date in=test1) mylib.test2 (keep=name date in=test2);

if test1 then source_table='test1';

else if test2 then source_table='test2';

run;

nicnad
Fluorite | Level 6

This is exactly what I needed it works great.

One thing is that I am using the set procedure on 10 different table. Is there an another then writing 10 if statements?

Can I just state something like source_table = in?

Also I see that the length of my variable source_table is set to 5 (with the first value being test1 I guess) so when afterwards I use a longer string it is truncated to 5 chars only. How do I solve this%

Thank you very much for your help and time.

ArtC
Rhodochrosite | Level 12

Getting away from the IF-THEN/ELSE is why the INDSNAME= option was added to the SET statement.

You can declare the variable's length using the LENGTH statement.  Right after the SET statement add something like:

     length source_table $7;

nicnad
Fluorite | Level 6

Thank you all for your replies!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 896 views
  • 6 likes
  • 3 in conversation