I think this is fairly straightforward; my brain just can't solve this problem right now:
A) I have a large SAS dataset of patients and it contains the villages that they each come from to get to clinic.
B) I have a long list of distances from the village to the clinic.
I need SAS to help me pull the distance from List B and put it into Dataset A.
How do I do that? Thank you!
This is a very common look up problem. One solution is to use Proc SQL to join things on a key value.
Here is a brief example.
data distance;
input village $ clinic $ distance;
datalines;
A Z 25
A Y 30
A X 60
B Z 18
B Y 28
B X 12
;
run;
data visit;
input patient Village $ Clinic $;
datalines;
1 A Z
2 A Y
3 B Z
4 B X
6 A Z
;
run;
proc sql;
create table matched as
select visit.patient, visit.Village, visit.clinic,
Distance.distance
from visit left join distance on
visit.village=distance.village and visit.clinic=distance.clinic
;
quit;
Note the use of the construct datasetname.variable to reference which table (dataset) a variable comes from.
Since I am matching on equal values the case of letters and spelling have to match exactly. NAME is not the same as Name.
So you may need to ensure that the spelling of the clinics and villages is the same everytime. If you think that the spelling is correct but may have differences is capital letter use then modfy the code to
upcase(visit.village)=upcase(distance.village) and upcase(visit.clinic)=upcase(distance.clinic)
This is a very common look up problem. One solution is to use Proc SQL to join things on a key value.
Here is a brief example.
data distance;
input village $ clinic $ distance;
datalines;
A Z 25
A Y 30
A X 60
B Z 18
B Y 28
B X 12
;
run;
data visit;
input patient Village $ Clinic $;
datalines;
1 A Z
2 A Y
3 B Z
4 B X
6 A Z
;
run;
proc sql;
create table matched as
select visit.patient, visit.Village, visit.clinic,
Distance.distance
from visit left join distance on
visit.village=distance.village and visit.clinic=distance.clinic
;
quit;
Note the use of the construct datasetname.variable to reference which table (dataset) a variable comes from.
Since I am matching on equal values the case of letters and spelling have to match exactly. NAME is not the same as Name.
So you may need to ensure that the spelling of the clinics and villages is the same everytime. If you think that the spelling is correct but may have differences is capital letter use then modfy the code to
upcase(visit.village)=upcase(distance.village) and upcase(visit.clinic)=upcase(distance.clinic)
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.