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

Good Afternoon everyone! So, I just realized that the following code doesn't give me perfect results and Im wondering what's going on.

 

data All_1;
 set both_ALL ;
 if (daterange_BZ = date_range) and (total_premium_biz = total_premium) then GoodMembers = 'Yes';
 if (daterange_BZ = date_range) and (total_premium_biz ^= total_premium) then SpanMatch = 'Yes';

 run;

daterange_BZ and date_range are both character variables and total_premium_biz/ total_premium are both numeric variables. When I see the results, for some records, goodmembers should be yes (meaning both set of variables match), but the code gives instead spanmatch as yes.

Any thoughts?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

For comparing numeric variables you may want to round them before comparison. Otherwise, small values can cause issues. 

View solution in original post

6 REPLIES 6
Reeza
Super User

For comparing numeric variables you may want to round them before comparison. Otherwise, small values can cause issues. 

devsas
Pyrite | Level 9

Ok thanks. I will check that.

SuryaKiran
Meteorite | Level 14

Hi,

 

Also look if there is a space issue with the character values. If there is a tab space after the string then both will not be same values. 

data need;

str1="compare ";/* TAB space after string*/
str2="compare";
if str1=str2;
run;

 

If there is a space issue, use 

(strip(daterange_BZ) = strip(date_range))
Thanks,
Suryakiran
Astounding
PROC Star

Character values may appear to match, but not actually match.  For example, there can be leading blanks, or even unseen characters such as carriage returns stored within a character variable.

 

Take an observation that you think should have matched on both sets of variables, and print the values:

 

put daterange_BZ $hex32.  ;

put date_range $hex32.;

 

See whether the values printed in hex format are actually a match or not.  Obviously (?), the solution varies, depending on where the problem lies.

devsas
Pyrite | Level 9

Thanks guys. I believe the problem was simply applying the round function in the comparison. I used the following code now and it seems to give me correct solutions.

data All_1;
 set both_ALL ;
 if (daterange_BZ = date_range) and round(total_premium_biz, .1) = round(total_premium, .1) then GoodMembers = 'Yes';
 if (daterange_BZ = date_range) and round(total_premium_biz, .1) ^=  round(total_premium, .1) then SpanMatch = 'Yes';
 run;
Reeza
Super User

You should make the second one an ELSE IF, not an IF. It's better from a logic standpoint and slightly more efficient because if the first statement evaluates as true it will skip the second statement.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 929 views
  • 0 likes
  • 4 in conversation