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

 I have a dataset A and want to compare mom vs mom_new and dad vs dad_new and comment

check_mom and check_dad. if the names are correct or wrong like follows

Mom  Dad    Mom_new    Dad_new    Check_mom       Check_dad

abc     dher     ABC             Dher           correct                correct

Bcd     sher     bcd             SHER           correct               correct

Fer      Sta       Far             Sta                wrong                correct

 

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Something like this perhaps:

data want;
   set have;
   if upcase(mom)=upcase(mom_new) then Check_mom='Correct';
   else Check_mom='Wrong';
run;

Same logic for dad.

 

Note: In the long run with SAS you may find that getting a True/False, Yes/No, Right/wrong comparison is better as the numeric 1 (True or yes or correct) and 0 (false no wrong ) in the long run. You can use a format to display any desired text but counting and reporting rates is often much easier with numeric values. Since SAS will return 1/0 as the result of logical comparisons the above code using that coding would be:

data want;
   set have;
   Check_mom = (upcase(mom)=upcase(mom_new));
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

Something like this perhaps:

data want;
   set have;
   if upcase(mom)=upcase(mom_new) then Check_mom='Correct';
   else Check_mom='Wrong';
run;

Same logic for dad.

 

Note: In the long run with SAS you may find that getting a True/False, Yes/No, Right/wrong comparison is better as the numeric 1 (True or yes or correct) and 0 (false no wrong ) in the long run. You can use a format to display any desired text but counting and reporting rates is often much easier with numeric values. Since SAS will return 1/0 as the result of logical comparisons the above code using that coding would be:

data want;
   set have;
   Check_mom = (upcase(mom)=upcase(mom_new));
run;
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

 

proc sql;
   create table want as
   select *, case when lowcase(mom) = lowcase(mom_new) then 'correct' else 'wrong' end as Check_mom
   from A;
   quit;

I am inferring from the example data that differences in letter case are not to be used to signify a non-match. If that is incorrect, then this code will fail.

The CASE WHEN statement should illustrate how to do this for the "mom" variables, and it should be simple to add another CASE WHEN statement for the other two variables that need this.

 

This could also be done with a DATA step with a different syntax.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 508 views
  • 1 like
  • 3 in conversation