BookmarkSubscribeRSS Feed
withpeople86
Calcite | Level 5

 1.png2.png3.png

 

 


I would like to make the third table using the first two tables. 

 

I thought of the following syntax:

 

proc sql; create table t1 as select *

from t3 a left join t2 b

on a.year=b.year and a.id=b.id;quit;

 

However, it only showed the first observation each year from the table 1.

 

Please help me to figure out.

4 REPLIES 4
novinosrin
Tourmaline | Level 20

pics or screen shots doesn't help. Please post the values as text

ballardw
Super User

@withpeople86 wrote:

 1.png2.png3.png

 

 


I would like to make the third table using the first two tables. 

 

I thought of the following syntax:

 

proc sql; create table t1 as select *

from t3 a left join t2 b

on a.year=b.year and a.id=b.id;quit;

 

However, it only showed the first observation each year from the table 1.

 

Please help me to figure out.


Which table is t3? which is t2?

 

What formats are currently involved with the variables? When you compare or join with = the values have to actually be equal. Numeric variables with decimals portions suppressed by "look" the same when examining tables but not be equal. If the variables are text you run into potential issues with leading characters.

 

did you try

proc sql; 
   create table t1 as 
   select a.*,b.var2,b.var3
   from t3 a left join t2 b
   on a.year=b.year and a.id=b.id;
quit;
mkeintz
PROC Star

Your table T! has one record with YEAR=3, ID=4.   And T2 has two such records, so T3 should also have two such records, but you posting shows only one.  If that  is an error, than this program takes advantage of the fact that both T1 and T2 are sorted by ID/YEAR:

 

data t1;
  input year id var1 :$1.;
datalines;
1 1 a
2 1 b
3 1 a
4 1 b
1 2 b
2 2 b
3 2 a
4 2 b
1 3 b
2 3 b
3 3 b
4 3 c
1 4 c
2 4 a
3 4 b
4 4 a
run;
data t2;
  input year id var2 var3;
datalines;
1 1 1 1
1 1 1 2
1 1 1 13
2 1 1 3
1 2 0 14
1 2 0 5
3 2 0 5
2 3 1 2
2 3 1 3
4 3 1 5
3 4 1 3
3 4 0 5
4 4 0 2
4 4 1 1
run;
data want;
  merge t1 t2;
  by id year;
run;

This program works because T1 has only 1 record for each YEAR/ID combination.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 815 views
  • 0 likes
  • 5 in conversation