BookmarkSubscribeRSS Feed
new_sas_user_4
Quartz | Level 8

I have a table “A” with details like subscriber_no, name, age, occupation

Eg of one of the records : Adam, 35, Data Analyst

And table “B” with similar info subscriber_no, name, age, occupation  and AN EXTRA COLUMN named CITY

Eg: Adam, 30, Director, Toronto

Ella, 40, Manager, Vancouver

 

I want the info in the final merged table as :

if the subscriber_no exists in table A, take all the information for that subscriber from table A

and just add the CITY : Toronto to it

say, Name : Adam, 35, Data Analyst, Toronto

 

I do not want the info from B at all EXCEPT adding CITY if the subscriber no exists in A.

However, if the subscriber no does not exist in A then I want all information about it from B

 

So the final tbale should look like:

Adam, 35, Data Analyst, Toronto

Ella, 40, Manager, Vancouver

 

How can I achieve this?

Thanks in advance:)

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

I don't see an actual value for subscriber_no in  your example data?

SuryaKiran
Meteorite | Level 14

Do a left join 

 

proc sql;
create table want as 
select a.*,b.city
from tableA a
left join tableB b on (a.name=b.name);
quit;

If your real data is joining by name, then what if you have multiple people with same names (mostly possible)?

Thanks,
Suryakiran
new_sas_user_4
Quartz | Level 8

Table A

001,Adam, 35, Data Analyst

 

Table B

001,Adam, 30, Director, Toronto

002, Ella, 40, Manager, Vancouver

 

I want Table C(final table) as 

001,Adam, 35, Data Analyst, Toronto

002, Ella, 40, Manager, Vancouver

 

 

Left join wont get me the subs from table B(which arent in table A)

there are no duplicate names/subscriber no

ballardw
Super User

@new_sas_user_4 wrote:

I have a table “A” with details like subscriber_no, name, age, occupation

Eg of one of the records : Adam, 35, Data Analyst

And table “B” with similar info subscriber_no, name, age, occupation  and AN EXTRA COLUMN named CITY

Eg: Adam, 30, Director, Toronto

Ella, 40, Manager, Vancouver

 

I want the info in the final merged table as :

if the subscriber_no exists in table A, take all the information for that subscriber from table A

and just add the CITY : Toronto to it

say, Name : Adam, 35, Data Analyst, Toronto

 

I do not want the info from B at all EXCEPT adding CITY if the subscriber no exists in A.

However, if the subscriber no does not exist in A then I want all information about it from B

 

So the final tbale should look like:

Adam, 35, Data Analyst, Toronto

Ella, 40, Manager, Vancouver

 

How can I achieve this?

Thanks in advance:)


Are the values for subscriber_no, name, age, occupation duplicated within table A? If not perhaps:

proc sort data=tablea;
   by subscriber_no name age occupation;
run;

proc sort data=tableb;
   by subscriber_no name age occupation;
run;

data want;
   update tablea
          tableb(keep = subscriber_no name age occupation city)
   ;
   by subscriber_no name age occupation;
run;
new_sas_user_4
Quartz | Level 8

The aboev solution is giving the error:

BY variable CITY is not on input data set WORK.A

 

Do both the data sets need to have the column CITY to use update statement?

 

Also, for the proc sort statement can I just use 

by subscriber_no

instead of subsriber_no name age occupation(all variables) ??

 

 

Reeza
Super User

This would be much easier to answer if you could provide a better sample input data set and expected output. You'll get a working answer much faster. 

 


@new_sas_user_4 wrote:

The aboev solution is giving the error:

BY variable CITY is not on input data set WORK.A

 

Do both the data sets need to have the column CITY to use update statement?

 

Also, for the proc sort statement can I just use 

by subscriber_no

instead of subsriber_no name age occupation(all variables) ??

 

 


 

new_sas_user_4
Quartz | Level 8

Please see the attached excel.

 

ballardw
Super User

@new_sas_user_4 wrote:

The aboev solution is giving the error:

BY variable CITY is not on input data set WORK.A

 

Do both the data sets need to have the column CITY to use update statement?

 

Also, for the proc sort statement can I just use 

by subscriber_no

instead of subsriber_no name age occupation(all variables) ??

 

 


Try MERGE instead of update.

new_sas_user_4
Quartz | Level 8

Using merge :

data C;

set A B;

run;

 

using this, it gives me tow rows for one subscriber 

1 Adam 35 Data Analyst 1 

1 Adam 40 Director          1 Toronto

 

I think its because it takes the unique data from both A and B and therefore returns two rows.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 2103 views
  • 0 likes
  • 5 in conversation