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

8 REPLIES 8
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;

 

raheleh22
Obsidian | Level 7

I am having trouble to know why my syntax not working properly. I am using a left join to join two tables based on 2 mutual coloumn ( Fipscode & Datetime) I made sure these two columns formatting are same before merging and here is my syntax: 

PROC SQL;
CREATE TABLE pamspi AS SELECT FROM PAMV1 LEFT JOIN spi12 ON pamV1.FIPScode=spi12.FIPScode AND pamv1.Datetime=spi12.Datetime;
QUIT;
proc print data=pamspi;
run;

 

PROC SQL;
CREATE TABLE pamspi AS SELECT * FROM PAMV1 LEFT JOIN spi12 ON pamV1.FIPScode=spi12.FIPScode AND pamv1.Datetime=spi12.Datetime;
QUIT;
proc print data=pamspi;
run;

and here is what happens: 

Screenshot 2024-08-01 122943.jpg

 

I see out of 143 obs I was expecting to be merged only 4 rows values have been merged and all others are showing missing as above. any thoughts on this? 

Kurt_Bremser
Super User

@raheleh22 wrote:

I am having trouble to know why my syntax not working properly. I am using a left join to join two tables based on 2 mutual coloumn ( Fipscode & Datetime) I made sure these two columns formatting are same before merging and here is my syntax: 

PROC SQL;
CREATE TABLE pamspi AS SELECT FROM PAMV1 LEFT JOIN spi12 ON pamV1.FIPScode=spi12.FIPScode AND pamv1.Datetime=spi12.Datetime;
QUIT;
proc print data=pamspi;
run;

 

PROC SQL;
CREATE TABLE pamspi AS SELECT * FROM PAMV1 LEFT JOIN spi12 ON pamV1.FIPScode=spi12.FIPScode AND pamv1.Datetime=spi12.Datetime;
QUIT;
proc print data=pamspi;
run;

and here is what happens: 

Screenshot 2024-08-01 122943.jpg

 

I see out of 143 obs I was expecting to be merged only 4 rows values have been merged and all others are showing missing as above. any thoughts on this? 


Very simple. You have only 4 matches.

Maxim 3: Know Your Data. This includes all data types, formats, and real values.

Are values left- or right-aligned (leading blanks!)?

Do such codes have leading zeroes in one dataset, but not the other (could be the result if codes were stored as numbers along the way)?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 8 replies
  • 1088 views
  • 10 likes
  • 5 in conversation