## Pulling a value from a list and matching it to an observation

Solved
Occasional Contributor
Posts: 17

# Pulling a value from a list and matching it to an observation

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!

Accepted Solutions
Solution
‎09-23-2016 12:54 PM
Super User
Posts: 13,583

## Re: Pulling a value from a list and matching it to an observation

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)

All Replies
Solution
‎09-23-2016 12:54 PM
Super User
Posts: 13,583

## Re: Pulling a value from a list and matching it to an observation

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 topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats