<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Difference between IF and WHERE statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192807#M36303</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If the datasets are supposed to represent similar data structures you need to go back further in your process and find out why one has the variable flag_n as text in some sets and numeric as others and address the issue at the "source".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Most likely culprit is a Proc Import step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The earlier a standard is set the easier the later processing goes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 22 Jan 2015 17:05:05 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2015-01-22T17:05:05Z</dc:date>
    <item>
      <title>Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192805#M36301</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input ID$ flag_c$ flag_n;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;A 0 0&lt;/P&gt;&lt;P&gt;B 1 1&lt;/P&gt;&lt;P&gt;C 2 2&lt;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*Where statement: put function sensitive*/&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result1a;set test;&lt;/P&gt;&lt;P&gt;where put(flag_c, $1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result1b;set test;&lt;/P&gt;&lt;P&gt;where put(flag_n, 1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*NOT working;&lt;/P&gt;&lt;P&gt;data result1c;set test;&lt;/P&gt;&lt;P&gt;where put(flag_c, 1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;*NOT working;&lt;/P&gt;&lt;P&gt;data result1d;set test;&lt;/P&gt;&lt;P&gt;where put(flag_n, $1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*IF statement: put function not sensitive*/&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result2a;set test;&lt;/P&gt;&lt;P&gt;if put(flag_c, $1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result2b;set test;&lt;/P&gt;&lt;P&gt;if put(flag_n, 1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result2c;set test;&lt;/P&gt;&lt;P&gt;if put(flag_c, 1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;*working;&lt;/P&gt;&lt;P&gt;data result2d;set test;&lt;/P&gt;&lt;P&gt;if put(flag_n, $1.) in ('0','1');&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was writing up a macro that has to subset a data set based on one flag. The flag can be character variable&lt;/P&gt;&lt;P&gt;in one data and numeric in another data. So I have to come up with a solution to handle both situations.&lt;/P&gt;&lt;P&gt;I decided to convert the flag (both character and numeric ones) into character variables using put function.&lt;/P&gt;&lt;P&gt;First, I tried where statement with put function. It doesn't work if the put function was not used properly or as intended&lt;/P&gt;&lt;P&gt;(see result1c , result1d).&lt;/P&gt;&lt;P&gt;Then, I tried if statement with put funciton. It looks like IF statement is not that sensentive to the proper and improper use of&lt;/P&gt;&lt;P&gt;put function and hanlded all the situations I may encounter.&lt;/P&gt;&lt;P&gt;MY question is: &lt;/P&gt;&lt;P&gt;How comes the difference between IF and WHERE statements in this case, why they act differently to &lt;/P&gt;&lt;P&gt;different (may be improper) uses of PUT function.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 16:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192805#M36301</guid>
      <dc:creator>jiangmi</dc:creator>
      <dc:date>2015-01-22T16:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192806#M36302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Where statements occur prior to the data being brought into the dataset.&lt;/P&gt;&lt;P&gt;If statements occur after the data is brought into the dataset.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since the if statement occurs after the data is brought into the dataset SAS, SAS can do an automatic conversion of the data.&lt;/P&gt;&lt;P&gt;You should get notes in your log about this, something like&lt;/P&gt;&lt;P&gt;NOTE: Numeric data converted to character data at line so many times&lt;/P&gt;&lt;P&gt;NOTE: Character data converted to numeric data at line so many times&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But, because the where statement occurs prior to the data being brought into the dataset, this automatic conversion can not occur.&lt;/P&gt;&lt;P&gt;So, you should get notes in your log, something like&lt;/P&gt;&lt;P&gt;NOTE: Data type must match in where statement&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm doing the notes from memory, so I'm sure they are not right.&lt;/P&gt;&lt;P&gt;But, you get the idea.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 16:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192806#M36302</guid>
      <dc:creator>BurntDirt</dc:creator>
      <dc:date>2015-01-22T16:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192807#M36303</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If the datasets are supposed to represent similar data structures you need to go back further in your process and find out why one has the variable flag_n as text in some sets and numeric as others and address the issue at the "source".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Most likely culprit is a Proc Import step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The earlier a standard is set the easier the later processing goes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 17:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192807#M36303</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-01-22T17:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192808#M36304</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, the first thing to realize is that the PUT function should probably be removed from the program entirely.&amp;nbsp; Most likely, it gives you the wrong answer even when there are no syntax errors.&amp;nbsp; For example, this combination produces a match:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;flag='123';&lt;/P&gt;&lt;P&gt;if put(flag, $1.) in ('0', '1');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's perfectly OK to code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if flag in ('0', '1');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;At least that's assuming this matches your intentions.&amp;nbsp; If you want flags that begin with "0" or with "1", you can still code it without a PUT function:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if flag in : ('0', '1');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second thing to realize is that this will not solve your problem.&amp;nbsp; To use a WHERE statement, you cannot compare numbers to characters.&amp;nbsp; An IF statement will convert one to the other, for purposes of making the comparison.&amp;nbsp; (You can check your log and see that a conversion took place for those cases.)&amp;nbsp; But the WHERE statement does not convert; it simply gives you the error you have already seen.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To solve this type of problem, you will need to write a more sophisticated macro.&amp;nbsp; Your macro will need to determine if your flag is numeric or character, and generate the appropriate WHERE statement.&amp;nbsp; There is more than one way to do this, but assuming you find one of them your macro will need to code the equivalent of:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%if (I found that FLAG is numeric in this particular data set) %then %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; where flag in (0, 1);&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%else %do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; where flag in ('0', '1');&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 17:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192808#M36304</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-01-22T17:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192809#M36305</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I was trying to answer your question about the difference between IF and WHERE.&lt;/P&gt;&lt;P&gt;But, there are some good points that have been brought up.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a semi-generic macro that checks the flag variable to determine if it is numeric or character and allows you to use a where statement,&lt;/P&gt;&lt;P&gt;which is more efficient for large datasets since it is done as the data is brought in and not after, and avoids the automatic conversions done by SAS.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro chkit(indata=test,outdata=result,flag=flag);&lt;/P&gt;&lt;P&gt;%let dsid=%sysfunc(open(&amp;amp;indata));&lt;BR /&gt;%let vartype = %sysfunc(vartype(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,&amp;amp;flag))));&lt;BR /&gt;%let rc=%sysfunc(close(&amp;amp;dsid));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data &amp;amp;outdata;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;indata;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;vartype = N %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where &amp;amp;flag in (0,1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where &amp;amp;flag in ('0','1');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend chkit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 18:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192809#M36305</guid>
      <dc:creator>BurntDirt</dc:creator>
      <dc:date>2015-01-22T18:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192810#M36306</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you all.&lt;/P&gt;&lt;P&gt;I will study your comments and give feed back later.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Jan 2015 22:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192810#M36306</guid>
      <dc:creator>jiangmi</dc:creator>
      <dc:date>2015-01-22T22:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between IF and WHERE statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192811#M36307</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;A good way to silently convert unknown variable to character is to use one of the CAT() functions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; background-color: #ffffff;"&gt;where cats(flag_n)&amp;nbsp; in ('0','1');&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Note that you will still get an error message if the FLAG_N variable does not exist. &lt;/P&gt;&lt;P&gt;You really should fix the data type issue earlier in your process.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 01:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-IF-and-WHERE-statements/m-p/192811#M36307</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-01-23T01:27:47Z</dc:date>
    </item>
  </channel>
</rss>

