DATA Step, Macro, Functions and more

What's wrong with my code

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 112
Accepted Solution

What's wrong with my code

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?


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 19,861

Re: What's wrong with my code

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

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 19,861

Re: What's wrong with my code

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

Frequent Contributor
Posts: 112

Re: What's wrong with my code

Ok thanks. I will check that.

Frequent Contributor
Posts: 141

Re: What's wrong with my code

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
Super User
Posts: 5,516

Re: What's wrong with my code

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.

Frequent Contributor
Posts: 112

Re: What's wrong with my code

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;
Super User
Posts: 19,861

Re: What's wrong with my code

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 115 views
  • 0 likes
  • 4 in conversation