<?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: if then else logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793564#M254367</link>
    <description>&lt;P&gt;Karolina,&lt;/P&gt;&lt;P&gt;You don't need to sort the data or count the parks you are looking for in a data step to get totals for (ACAD and ARCH) when you use reporting procedures and include a where clause you have all the info. PROC FREQ maybe easier as shown in PaigeMiller reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I like to use PROC TABULATE. What I did was to make a small data set with park names and ran the tabulate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC TABULATE DATA=parks (WHERE=(park IN("ACAD" "ARCH"))) NOSEPS MISSING;
  CLASS park;
  TABLE PARK ALL
        ,
        N
        /RTS=20 ROW=FLOAT MISSTEXT=' ';
RUN;  &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;OUTPUT:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jbilenas_0-1643650792359.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68038iED8E2F28C3B95D75/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jbilenas_0-1643650792359.png" alt="jbilenas_0-1643650792359.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I am a Tabulate and FORMAT geek so I also created the following code that includes all parks; results for ACAD, ARCH, and all other parks combined:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;PROC FORMAT;
  VALUE $PARK (MULTILABEL NOTSORTED)
    'ARCH', 'ACAD' = 'TOTAL for ARCH and ACAD'
    'ARCH' = 'ARCH'
    'ACAD' = 'ACAD'
     OTHER = 'OTHER PARKS'
  ;
  PICTURE pct(round) 
    low -&amp;lt;0   = '009.00%' (PREFIX='-')
    0   -high = '009.00%' (PREFIX=' ')
  ; 
RUN;

PROC TABULATE DATA=parks NOSEPS MISSING;
  CLASS park/MLF ORDER=DATA;
  FORMAT park $PARK.;
  TABLE PARK ALL
        ,
        N
        PCTN = '%'*F=pct.
        /RTS=30 ROW=FLOAT MISSTEXT=' ';
RUN; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OUTPUT:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jbilenas_1-1643651149603.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68039i79D5E90F7A0B3C69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jbilenas_1-1643651149603.png" alt="jbilenas_1-1643651149603.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Full code should be attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jonas V. Bilenas&lt;/P&gt;</description>
    <pubDate>Mon, 31 Jan 2022 17:54:41 GMT</pubDate>
    <dc:creator>jbilenas</dc:creator>
    <dc:date>2022-01-31T17:54:41Z</dc:date>
    <item>
      <title>if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793530#M254357</link>
      <description>&lt;P&gt;dear community,&lt;/P&gt;
&lt;P&gt;I have a quesiton if you would like to select only specific parks (ACAD and ARCH) and count them, and the other parks not. What is wrong in this code below? pls advise.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="touwen_k_0-1643642330071.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68028i353894D3920843F3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="touwen_k_0-1643642330071.png" alt="touwen_k_0-1643642330071.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;regards,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Karolina&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 15:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793530#M254357</guid>
      <dc:creator>touwen_k</dc:creator>
      <dc:date>2022-01-31T15:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793532#M254358</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.parkcode in ("ACAD","ARCH") then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will not work, because the special variables that begin with first. are always either 0 or 1. They are zero when it is not the first observation of a BY variable, and it is 1 if it is the first obsesrvation of a BY variable. So whether it is 0 or 1, it will never be in ("ACAD","ARCH")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you really want to do is this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=pg2.np_species (where=(parkcode in ('ACAD','ARCH')));
    tables parkcode;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Advice: don't try to write your own DATA step code for common activities like counting, computing sums and means and standard deviations, finding minimums and maximums, and so forth. SAS has written many PROCs where they have already programmed these things, then they debug the code, verify that it works, and these PROCs have been proven in a bazillion and 14 real world applications. If you write your own DATA step code, then you have to vouch for the correctness of the results, and as you can see, you also have to overcome the difficulties of programming these things in a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From now on, please do not post your code as a screen capture, post your code &lt;FONT color="#FF0000"&gt;as text&lt;/FONT&gt;, in the code box as I have done, by clicking on the "little running man" icon. Also, please, from now on, when your code doesn't work, show us the entire LOG for the step that isn't working (not selected parts of the log) &lt;FONT color="#FF0000"&gt;as text&lt;/FONT&gt;, by clicking on the &amp;lt;/&amp;gt; icon and pasting your log text into that window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 15:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793532#M254358</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-01-31T15:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793540#M254361</link>
      <description>&lt;P&gt;As Paige has explained first. or last. on a by statement returns a 0 or 1 indicating true or false. If your objective is to count the records as you are reading the data set you can use the code below. However if your objective is to get counts of each by group then you can use any of the procs that SAS has.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_ds;
set species_sort end=last;
by parkcode;
retain count;
if first.parkcode then do; count=0; end;
count+1;
if last.parkcode then do;
put parkcode= count=;
if last then do;
put _n_=;
end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 Jan 2022 16:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793540#M254361</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2022-01-31T16:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793564#M254367</link>
      <description>&lt;P&gt;Karolina,&lt;/P&gt;&lt;P&gt;You don't need to sort the data or count the parks you are looking for in a data step to get totals for (ACAD and ARCH) when you use reporting procedures and include a where clause you have all the info. PROC FREQ maybe easier as shown in PaigeMiller reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I like to use PROC TABULATE. What I did was to make a small data set with park names and ran the tabulate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC TABULATE DATA=parks (WHERE=(park IN("ACAD" "ARCH"))) NOSEPS MISSING;
  CLASS park;
  TABLE PARK ALL
        ,
        N
        /RTS=20 ROW=FLOAT MISSTEXT=' ';
RUN;  &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;OUTPUT:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jbilenas_0-1643650792359.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68038iED8E2F28C3B95D75/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jbilenas_0-1643650792359.png" alt="jbilenas_0-1643650792359.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I am a Tabulate and FORMAT geek so I also created the following code that includes all parks; results for ACAD, ARCH, and all other parks combined:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;PROC FORMAT;
  VALUE $PARK (MULTILABEL NOTSORTED)
    'ARCH', 'ACAD' = 'TOTAL for ARCH and ACAD'
    'ARCH' = 'ARCH'
    'ACAD' = 'ACAD'
     OTHER = 'OTHER PARKS'
  ;
  PICTURE pct(round) 
    low -&amp;lt;0   = '009.00%' (PREFIX='-')
    0   -high = '009.00%' (PREFIX=' ')
  ; 
RUN;

PROC TABULATE DATA=parks NOSEPS MISSING;
  CLASS park/MLF ORDER=DATA;
  FORMAT park $PARK.;
  TABLE PARK ALL
        ,
        N
        PCTN = '%'*F=pct.
        /RTS=30 ROW=FLOAT MISSTEXT=' ';
RUN; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OUTPUT:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jbilenas_1-1643651149603.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68039i79D5E90F7A0B3C69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jbilenas_1-1643651149603.png" alt="jbilenas_1-1643651149603.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Full code should be attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jonas V. Bilenas&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 17:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793564#M254367</guid>
      <dc:creator>jbilenas</dc:creator>
      <dc:date>2022-01-31T17:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793750#M254451</link>
      <description>&lt;P&gt;Thank you all for your advices. I have now code that works, it counts the occurences for parks ACAD and ARCH and for other parks the count is zero.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data count;
set species_sort;
by parkcode;
if first.parkcode and parkcode in ('ACAD', 'ARCH') then
count=0;
count+1;
if parkcode not in ('ACAD', 'ARCH') then count=0;
if last.parkcode then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Feb 2022 13:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793750#M254451</guid>
      <dc:creator>touwen_k</dc:creator>
      <dc:date>2022-02-01T13:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: if then else logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793752#M254452</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/322058"&gt;@touwen_k&lt;/a&gt;&amp;nbsp;again I recommend you use PROC FREQ here instead of struggling with DATA step code.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Feb 2022 13:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then-else-logic/m-p/793752#M254452</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-01T13:21:45Z</dc:date>
    </item>
  </channel>
</rss>

