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

Hi, 

I am using proc sql left join to merge my two excels on based on two mutual columns they have. 

here is the code I use: 

proc sql;
CREATE TABLE FINALV3 AS SELECT * FROM WNPOP LEFT JOIN pop2012 ON WNPOP.State_Name_All=pop2012.State_Name_All AND WNPOP.County_Name_All=pop2012.County_Name_All;
QUIT;

while the proc happened and I can see the columns joined and the number of obs are correct, I do not have values in the joined columns. (THERE IS NO ERROR in the LOG ). 

any ideas of what is going wrong? 

Screenshot 2024-06-01 142840.jpg

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@raheleh22 The screenshot indicates that none of the rows from the "left" table joins with the "main" table. 

Often such "issues" occur when there are leading blanks or especially differences in casing. Try if below code returns the desired result.

proc sql feedback;
  create table finalv3 as 
    select *
    from wnpop l
    left join pop2012 r 
      on    upcase(strip(wnpop.state_name_all)) =upcase(strip(pop2012.state_name_all))
        and upcase(strip(wnpop.county_name_all))=upcase(strip(pop2012.county_name_all))
  ;
quit;

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Looks like no match was found.

Inspect your data for the values of your key variables. Exact spelling, upper/lowercase, leading blanks.

Also stop using the asterisk, it will cause at least WARNINGs for the key variables, and it might also be the cause of your problem if variables are present in both datasets.

 

raheleh22
Obsidian | Level 7

the mutual columns in these two tables are county_name_all and state_name_all and that is why i am doing the join based on these two. I have double checked and they are same in every thing in both excels. i even check and their excel format are same too. but still I have the problem 

Kurt_Bremser
Super User

You constantly talk about "Excels", but this here are the SAS communities, where datasets are used.

Don't trust anything you see in Excel. Inspect the datasets after import in SAS, using SAS means, e.g. displaying character values with $HEX formats to reveal hidden characters and the like.

 

If a join "does not work", then the key values do not match, period.

Patrick
Opal | Level 21

@raheleh22 The screenshot indicates that none of the rows from the "left" table joins with the "main" table. 

Often such "issues" occur when there are leading blanks or especially differences in casing. Try if below code returns the desired result.

proc sql feedback;
  create table finalv3 as 
    select *
    from wnpop l
    left join pop2012 r 
      on    upcase(strip(wnpop.state_name_all)) =upcase(strip(pop2012.state_name_all))
        and upcase(strip(wnpop.county_name_all))=upcase(strip(pop2012.county_name_all))
  ;
quit;

 

ballardw
Super User

Without actual values from the data set we cannot tell what actually happens.

The two most common causes for text values not matching are case, such as ALL UPPER CASE letters not matching Some Upper Case letters. The other is presence of leading spaces in one or more of the variables.

 

Try

proc sql;
CREATE TABLE FINALV3 AS 
SELECT Wnpop.* , pop2012.*
FROM WNPOP LEFT JOIN pop2012 ON upcase(strip(WNPOP.State_Name_All))=upcase(strip(pop2012.State_Name_All)) AND upcase(strip(WNPOP.County_Name_All))=upcase(strip(pop2012.County_Name_All));
QUIT;

Specifically related to County names in different source files sometimes they will actually have the word "County" and sometimes not. So that is something to examine as well as "Ada" does not match "Ada County".

mariangela86
Fluorite | Level 6

The pop2012 fields used in the join have the suffix "_ALL": pop2012.Country_Name_All pop2012.State_Name_All , but in the picture, the table "finalv13" has pop2012 FIELDS, WITHOUT SUFFIX "_ALL"! 

mariangela86_0-1717363691702.png

 

Correct the name of the two fields in the join:

pop2012.Country_Name
pop2012.State_NAME

Below an example code with alias for tables, with upcase(converts all lowercase letters to uppercase letters) and compress(remove blanks) functions for variables:

 

proc sql;
CREATE TABLE FINALV3 AS
SELECT
t1.* ,
t2.State_NAME,
t2.County_Name, t2.POP_2012
FROM WNPOP t1 LEFT JOIN pop2012 t2
ON
compress(upcase(t1.State_Name_All))=compress(upcase(t2.State_NAME)) AND
compress(upcase(t1.County_Name_All))=compress(upcase(t2.County_Name));
QUIT;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 483 views
  • 9 likes
  • 5 in conversation