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?
For comparing numeric variables you may want to round them before comparison. Otherwise, small values can cause issues.
For comparing numeric variables you may want to round them before comparison. Otherwise, small values can cause issues.
Ok thanks. I will check that.
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))
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.
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;
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.