11-07-2017 05:25 PM
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.
11-07-2017 06:40 PM
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.
str1="compare ";/* TAB space after string*/
If there is a space issue, use
(strip(daterange_BZ) = strip(date_range))
11-07-2017 06:46 PM
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.
11-08-2017 11:09 AM
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;
11-08-2017 11:12 AM
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.
Need further help from the community? Please ask a new question.