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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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)

View solution in original post

1 REPLY 1
ballardw
Super User

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)

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1336 views
  • 2 likes
  • 2 in conversation