BookmarkSubscribeRSS Feed
makset
Obsidian | Level 7

Hello

How can I add two variables from two dataset?
As the picture.

I am currently using the rename function and join dataset in one and adds variables. Seeking a less troublesome solution.





I have another question
How to change this command to create a dataset with the result of the command.


from: http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/viewer.htm#a001361224.htm

[First or second table, but not both]

There is no keyword in PROC SQL that returns unique rows from the first and second table, but not rows that occur in both. Here is one way you can simulate this operation:

(query1 except query2) union (query2 except query1)

This example shows how to use this operation.

proc sql;
 title 'A EXCLUSIVE UNION B';
 (select * from sql.a except select * from sql.b)
 union 
(select * from sql.b except select * from sql.a);


Thank you for your help.

2 REPLIES 2
DBailey
Lapis Lazuli | Level 10

For the first part, if you're joining by id, then its pretty straightforward depending on which table (if any) would be the "master" table.  If neither, then you might consider:

proc sql;

create table want as

select

     coalesce(t1,id, t2.id) as ID,

     coalesce(t1.a,0) + coalesce(t2.a,0) as a

from

     dataset1 t1

     full outer join dataset2 t2

          on t1.id=t2.id;

quit;

you will have some issues if id is not unique in either table.

For the second part (without testing):

proc sql;

create table want as

select t1.* from (

     (select * from sql.a except select * from sql.b)

     union

     (Select * from sql.b except select * from sql.a)

) t1;

quit;

stat_sas
Ammonite | Level 13

proc sql;

create table three as

select first.id,sum(first.a,second.a) as a

from first inner join second

on first.id=second.id;

quit;

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