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

Hi,

I have a data set "world" which include 256 countries and continent. And another data set "have" include 125 countries and other variable. Now I want to attach the continent information to data"have".

So my code is

 

proc sort data=world;
by country;
run;

 

proc sort data=have;
by country;
run;

 

data attached;
merge have world;
by country;
run;

 

 

And the result is something like

   country        variables      continent

Armenia             22                 .

2  China                 15                   .

3  Korea               5                   .

...

126  Armenia         .                 Asia

127  China            .                 Asia

128  Korea            .                 Asia

 

What I want is

 country        variables      continent

Armenia             22                 Asia

2  China                 15                Asia

3  Korea               5                   Asia

...

 

Does my code have problem ?? Anyone could modify my code to get what I want? 

 

THANK YOU

1 ACCEPTED SOLUTION

Accepted Solutions
YangYY
Quartz | Level 8

I have found a solution with using the strip function to remove the leading blank. So SAS can recognize two countries variables are the same and the merge would work.  

View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

It looks like the country names don't really match. Maybe you have invisible characters (tabs?) or leading spaces as part of the country names in one of the datasets.

PG
YangYY
Quartz | Level 8

I checked the output data and I did not see the countries have leading space(for both of them). How should I modify my code to solve this problem??

 

Thank you

ed_sas_member
Meteorite | Level 14

Have you checked if your country variable has a format?

 

you can run a proc contents to see that as follows :

proc contents data= have ;
run;
proc contents data= world ;
run;
YangYY
Quartz | Level 8

I don't think they have a format. What format they are supposed to have? How should I add a format to them?

Kurt_Bremser
Super User

@YangYY wrote:

I don't think they have a format.


Don't think. KNOW. Run proc contents. This will reveal types, lengths and formats.

 

If you do a join or merge on character variables, they must have identical values. This means the join is case-sensitive.

How did you inspect your columns? The best way to find erratic characters is to create a new variable where you store the hex coding.

Suppose your country column has a length of 10, then do this (in both datasets):

length country_hex $20;
country_hex = put(country, $hex20.);

Then you can have a very clear picture of the contents.

Astounding
PROC Star
Examine the results by running a PROC PRINT without then with this statement:

format country $hex32.;
YangYY
Quartz | Level 8

I have found a solution with using the strip function to remove the leading blank. So SAS can recognize two countries variables are the same and the merge would work.  

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 2190 views
  • 1 like
  • 5 in conversation