BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Daily1
Quartz | Level 8

update sample1 from another sample2 in sas

match id and code then exp update 

example.363823 and 52 matched then put 5.

i want this type of query ..Pls help

sample data 

data sample1;
  input ID 1-6 code 7-9 exp 10-11;
  datalines;
363823	52	0
363823	53	0
293556	50	0
293556	51	0
293556	52	0
293556	53	0
;

data sample2;
  input ID 1-6 code 7-9 exp 10-11;
  datalines;
363823	52	5
363823	53	8
293556	50	9
;

 I want this type of output

Daily1_0-1675272549675.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Daily1 wrote:
proc sql;
update sample1 set exp= select n.exp from sample2 as n 
where n.id=sample1.id and n.code =sample1.code;
quit;

Please Help


Did you mean

update sample1 
set exp= 
 (select n.exp from sample2 n 
  where n.id=sample1.id and n.code =sample1.code
 )
where exists (select n.exp from sample2 n 
  where n.id=sample1.id and n.code =sample1.code
 )
;

View solution in original post

11 REPLIES 11
yabwon
Amethyst | Level 16

Looks like you are doing a homework. 

 

step 1) use Proc Sort and sort both datasets by ID and Code. 

step 2) use Merge statement to combine both data sets by ID and Code, remember to use them in the Merge statement in order "sample1 sample2" so the values of exp variable form the second one will overwrite values of exp from the first one (only in case when there is a match by ID and Code of course)

 

try to do it yourself, in case of problems - ask 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Daily1
Quartz | Level 8
proc sql;
update sample1 set exp= select n.exp from sample2 as n 
where n.id=sample1.id and n.code =sample1.code;
quit;

Please Help

Daily1
Quartz | Level 8

Daily1_0-1675277118002.png

Sql Code 

Daily1_1-1675277271407.png

Result

Daily1_2-1675277348568.png

this code not working on sas eg ...please help 

Tom
Super User Tom
Super User

That photograph does not appear to be proper SQL syntax for how the UPDATE statement works.

https://en.wikipedia.org/wiki/Update_(SQL)

 

The UPDATE statement has the following form:

UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]

Perhaps it is some extension provided by some other database language you have used in the past?

 

Daily1
Quartz | Level 8
proc sql;
update sample1 set exp= (select n.exp from sample2 as n )
where n.id=sample1.id and n.code =sample1.code;
quit;

log
proc sql;
29 update sample1 set exp= (select n.exp from sample2 as n )
30 where n.id=sample1.id and n.code =sample1.code;
ERROR: Unresolved reference to table/correlation name n.
ERROR: Unresolved reference to table/correlation name n.
ERROR: Expression using equals (=) has components that are of different data types.
ERROR: Expression using equals (=) has components that are of different data types.
31 quit;

Daily1
Quartz | Level 8
proc sql;
update sample1 set exp= (select n.exp from sample2 n )
where n.id=sample1.id and n.code =sample1.code;
quit;

Please correct query
Tom
Super User Tom
Super User

@Daily1 wrote:
proc sql;
update sample1 set exp= select n.exp from sample2 as n 
where n.id=sample1.id and n.code =sample1.code;
quit;

Please Help


Did you mean

update sample1 
set exp= 
 (select n.exp from sample2 n 
  where n.id=sample1.id and n.code =sample1.code
 )
where exists (select n.exp from sample2 n 
  where n.id=sample1.id and n.code =sample1.code
 )
;
Tom
Super User Tom
Super User

Why not just skip the SQL and use normal SAS code?

The UPDATE statement is for applying transactions to existing datasets.

data sample1;
  input ID code exp ;
datalines;
293556	50	0
293556	51	0
293556	52	0
293556	53	0
363823	52	0
363823	53	0
;

data sample2;
  input ID code exp ;
datalines;
293556	50	9
363823	52	5
363823	53	8
;

/*
proc sort data=sample1; by id code; run;
proc sort data=sample2; by id code; run;
*/

data want;
  update sample1 sample2;
  by id code ;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 9389 views
  • 1 like
  • 4 in conversation