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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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