BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi community,

 

While I write the below query in SQL : It update the data in the required column.

update Tab1
set ht = b.height
from Tab1 a join Tab2 b
on a.ID = b.p_ID and ht is null and
b.record_date = (select min(record_date) from Tab2 cc where a.ID = cc.p_ID) 

The above code represents : where ever the ht is null (in Tab1) - there we should update minimum record_date value of Height from Tab2.

The same code I'm trying to do in PROC SQL.

 

The update code in proc sql is quiet different from the above code.

I tried, but I end up with some errors. Could anyone please help to suggest a correct code to resolve this issue.

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You need to put the whole code that selects the correct value to use into the right side of the set, and only the one condition that selects the value to be replaced has to stay on the main level:

data tab1;
input id ht;
cards;
1 10
2 .
;
run;

data tab2;
input p_id record_date :yymmdd10. height;
format record_date yymmddd10.;
cards;
1 2018-10-18 10
1 2018-10-20 12
2 2018-10-18 12
2 2018-10-20 13
;
run;

proc sql;
update Tab1 a
set ht = (
  select b.height
  from tab2 b
  where a.ID = b.p_ID and b.record_date = (select min(record_date) from Tab2 c where a.ID = c.p_ID)
)
where ht is null;
quit;

Note the two data steps supplying example/test data. Use such in the future to illustrate your issue. Keep in mind that code is always driven by data, so it is essential to know the data (Maxim 3).

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

You need to put the whole code that selects the correct value to use into the right side of the set, and only the one condition that selects the value to be replaced has to stay on the main level:

data tab1;
input id ht;
cards;
1 10
2 .
;
run;

data tab2;
input p_id record_date :yymmdd10. height;
format record_date yymmddd10.;
cards;
1 2018-10-18 10
1 2018-10-20 12
2 2018-10-18 12
2 2018-10-20 13
;
run;

proc sql;
update Tab1 a
set ht = (
  select b.height
  from tab2 b
  where a.ID = b.p_ID and b.record_date = (select min(record_date) from Tab2 c where a.ID = c.p_ID)
)
where ht is null;
quit;

Note the two data steps supplying example/test data. Use such in the future to illustrate your issue. Keep in mind that code is always driven by data, so it is essential to know the data (Maxim 3).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 1764 views
  • 1 like
  • 2 in conversation