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

Hello,all:

 

I have some questions:

 

1, I would like to compare two data sets, some variables has the same name but different positions, how to set a flag to find them, and how to ignore.   For example, the variable “ID” has the name of “John” but one is at the left of the column and the other is at the center of the column. 

  1. If sometimes “John” is all in capital “JOHN”, and the other case is in “john”. How to let the system know they refer the same person, and it could be ignore.

With all above, which statement I should be use? Please advice, thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

Hello,all:

 

I have some questions:

 

1, I would like to compare two data sets, some variables has the same name but different positions, how to set a flag to find them, and how to ignore.   For example, the variable “ID” has the name of “John” but one is at the left of the column and the other is at the center of the column. 

  1. If sometimes “John” is all in capital “JOHN”, and the other case is in “john”. How to let the system know they refer the same person, and it could be ignore.

With all above, which statement I should be use? Please advice, thanks.


First, you have to merge the two datasets, using the MERGE statement of a data step, or using PROC SQL.

 

Capitalization is easy to deal with, just compare the all caps (or all lower case) version of the text, in other words, you can compare UPPER(variable1) to UPPER(variable2). If the text isn't always at the left hand side, you can use the LEFT function to force the text to be at the left hand side. You also want to use the TRIM function to get rid of trailing blanks which could cause the comparison to fail.

 

so, a data step comparison would look something like

 

if trim(left(upper(variable1))) = trim(left(upper(variable2))) then ...

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

Hello,all:

 

I have some questions:

 

1, I would like to compare two data sets, some variables has the same name but different positions, how to set a flag to find them, and how to ignore.   For example, the variable “ID” has the name of “John” but one is at the left of the column and the other is at the center of the column. 

  1. If sometimes “John” is all in capital “JOHN”, and the other case is in “john”. How to let the system know they refer the same person, and it could be ignore.

With all above, which statement I should be use? Please advice, thanks.


First, you have to merge the two datasets, using the MERGE statement of a data step, or using PROC SQL.

 

Capitalization is easy to deal with, just compare the all caps (or all lower case) version of the text, in other words, you can compare UPPER(variable1) to UPPER(variable2). If the text isn't always at the left hand side, you can use the LEFT function to force the text to be at the left hand side. You also want to use the TRIM function to get rid of trailing blanks which could cause the comparison to fail.

 

so, a data step comparison would look something like

 

if trim(left(upper(variable1))) = trim(left(upper(variable2))) then ...

 

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

How to write the command?

 

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 778 views
  • 2 likes
  • 3 in conversation