BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tmdgus
Fluorite | Level 6

Please refer to two fake datasets

 

Data 1:

YearstatecityconditionAconditionBValue
202100115000
202100124000
20210013900
20211121.
202101223000
202101232980

 

Data 2:

cityconditonAconditionBValue
0124100
1233000

 

Final Table:

YearstatecityconditionAconditionBValue
202100115000
202100124100
20210013900
20211121.
202101223000
202101233000

 

Basically I want to replace value from data 2 to data 1.

 

Which method should be used? (data step/proc SQL)

 

Thanks in advance.,

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Do year and State not matter? You can have cities with the same name in multiple states so unless these are IDs I can see this being problematic.

If you had year/state included I would say use a data step UPDATE but if you don't, use a SQL join and COLESCE().

proc sql;
create table want as
select t1.*, coalesce(t2.Value, t1.Value) as new_value
from data1 as t1
left join data2 as t2
on t1.city=t2.city and t1.conditionA=t2.ConditionA and t1.ConditionB=t2.ConditionB;
quit;

@tmdgus wrote:

Please refer to two fake datasets

 

Data 1:

Year state city conditionA conditionB Value
2021 0 0 1 1 5000
2021 0 0 1 2 4000
2021 0 0 1 3 900
2021 1 1 2 1 .
2021 0 1 2 2 3000
2021 0 1 2 3 2980

 

Data 2:

city conditonA conditionB Value
0 1 2 4100
1 2 3 3000

 

Final Table:

Year state city conditionA conditionB Value
2021 0 0 1 1 5000
2021 0 0 1 2 4100
2021 0 0 1 3 900
2021 1 1 2 1 .
2021 0 1 2 2 3000
2021 0 1 2 3 3000

 

Basically I want to replace value from data 2 to data 1.

 

Which method should be used? (data step/proc SQL)

 

Thanks in advance.,




View solution in original post

2 REPLIES 2
Reeza
Super User

Do year and State not matter? You can have cities with the same name in multiple states so unless these are IDs I can see this being problematic.

If you had year/state included I would say use a data step UPDATE but if you don't, use a SQL join and COLESCE().

proc sql;
create table want as
select t1.*, coalesce(t2.Value, t1.Value) as new_value
from data1 as t1
left join data2 as t2
on t1.city=t2.city and t1.conditionA=t2.ConditionA and t1.ConditionB=t2.ConditionB;
quit;

@tmdgus wrote:

Please refer to two fake datasets

 

Data 1:

Year state city conditionA conditionB Value
2021 0 0 1 1 5000
2021 0 0 1 2 4000
2021 0 0 1 3 900
2021 1 1 2 1 .
2021 0 1 2 2 3000
2021 0 1 2 3 2980

 

Data 2:

city conditonA conditionB Value
0 1 2 4100
1 2 3 3000

 

Final Table:

Year state city conditionA conditionB Value
2021 0 0 1 1 5000
2021 0 0 1 2 4100
2021 0 0 1 3 900
2021 1 1 2 1 .
2021 0 1 2 2 3000
2021 0 1 2 3 3000

 

Basically I want to replace value from data 2 to data 1.

 

Which method should be used? (data step/proc SQL)

 

Thanks in advance.,




SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1030 views
  • 1 like
  • 2 in conversation