<?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: Simplify multiple IF blocks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828005#M327073</link>
    <description>&lt;P&gt;It would help a lot if you could not only provide some sample input and output data, but also explain in plain English the process this DATA step is part of. Where is this data being sourced and what is it being used for? Not having any business context makes it even harder to figure out what it is doing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Aug 2022 00:32:06 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2022-08-10T00:32:06Z</dc:date>
    <item>
      <title>Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/827915#M327035</link>
      <description>&lt;P&gt;May I know how can we simplify this step? Multiple IF blocks are confusing and also the retain statement. If we cant simplify this step, can someone explain this code in simple terms? I understand the SAS functions but I'm not getting the whole picture here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA WORK.BASELINE;
       SET WORK.BASELINE_;
       BASSTRTM2 = INPUT(substr(BSSTRTM,1,2)||':'||substr(BSSTRTM,4,2)||':'||SUBSTR(BSSTRTM,7,2),TIME5.);
       BASFINTM2 = INPUT(substr(BASFINTM,1,2)||':'||substr(BASFINTM,4,2)||':'||SUBSTR(BASFINTM,7,2),TIME5.);
       
       BASSTRTDTTM = DHMS(BAS_STRT_DT,0,0,BASSTRTM2);
       BASFINDTTM = DHMS(BAS_FINISH_DT,0,0,BASFINTM2);
       POSTDTTM = DHMS(POSTNG_DT,0,0,0);
       
       BASEHRS = INTCK('HOURS',BASSTRTDTTM,BASFINDTTM,'continuous');
       IF BASEHRS &amp;lt; 1 THEN BASEHRS = 1;
       IF BAS_STRT_DT = POSTNG_DT THEN
       DO;
              IF DAYS_OF_PO = 1 THEN
                     DO;
                           INITIALHRS = BASEHRS;
                           NEWBASEHRS=INITIALHRS;
                           DAYHRS = INITIALHRS;
                     END;
              ELSE
                     DO;
                           INITIALHRS = INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS');
                           DAYHRS = INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS');
                           NEWBASEHRS=BASEHRS-INITIALHRS;
                     END;
       END;
       ELSE IF BAS_FINISH_DT = POSTNG_DT THEN  
              DO;
              DAYHRS = NEWBASEHRS;
              END;
       ELSE 
              DO;
              DAYHRS = 24;
              NEWBASEHRS=NEWBASEHRS-DAYHRS;
              END;
       RETAIN INITIALHRS;
       RETAIN NEWBASEHRS;
       RUN;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Aug 2022 16:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/827915#M327035</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-08-09T16:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828001#M327071</link>
      <description>&lt;P&gt;Example data. Source and expected result and possibly a description of the problem to really provide any meaningful difference in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This stuff looks like it would like to read a time value that was written with something other than : as the original delimiter.&lt;/P&gt;
&lt;P&gt;Without an example of the source value not much that can be suggested.&lt;/P&gt;
&lt;PRE&gt;       BASSTRTM2 = INPUT(substr(BSSTRTM,1,2)||':'||substr(BSSTRTM,4,2)||':'||SUBSTR(BSSTRTM,7,2),TIME5.);
       BASFINTM2 = INPUT(substr(BASFINTM,1,2)||':'||substr(BASFINTM,4,2)||':'||SUBSTR(BASFINTM,7,2),TIME5.);
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see much way to address your "ifs". Your "multiple" are pretty much 2 at each point. So not much to reduce.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code apparently is calculating some sort of offset from some date condition that is reset when one of the values reaches 1. The RETAIN means values available from the first reset are available until reset. Not much of any other way if you need those variables for calculations on the right side of the =.&amp;nbsp; I am not sure that the Initialhrs needs to be retained from the code I see but the Newbasehrs is used to set Dayhrs.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 22:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828001#M327071</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-09T22:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828002#M327072</link>
      <description>So not much can be simplified without seeing the data?&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Aug 2022 23:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828002#M327072</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-08-09T23:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828005#M327073</link>
      <description>&lt;P&gt;It would help a lot if you could not only provide some sample input and output data, but also explain in plain English the process this DATA step is part of. Where is this data being sourced and what is it being used for? Not having any business context makes it even harder to figure out what it is doing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 00:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828005#M327073</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-08-10T00:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828013#M327074</link>
      <description>Well, can someone help me understand that ELSE IF block?&lt;BR /&gt;</description>
      <pubDate>Wed, 10 Aug 2022 02:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828013#M327074</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-08-10T02:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828015#M327075</link>
      <description>&lt;P&gt;A question:&amp;nbsp;why use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       ELSE IF BAS_FINISH_DT = POSTNG_DT THEN  
              DO;
              DAYHRS = NEWBASEHRS;
              END;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       ELSE IF BAS_FINISH_DT = POSTNG_DT THEN DAYHRS = NEWBASEHRS;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You apparently can't drop the RETAIN statements, because there are some conditions in which a new value for INITIALHRS or NEWBASEHRS will not be calculated, and you apparently want to carry forward the previous values instead of resetting them to missing..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now,&amp;nbsp;if you're really just trying to reduce lines of code, you could change&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       IF BAS_STRT_DT = POSTNG_DT THEN
       DO;
              IF DAYS_OF_PO = 1 THEN
                     DO;
                           INITIALHRS = BASEHRS;
                           NEWBASEHRS=INITIALHRS;
                           DAYHRS = INITIALHRS;
                     END;
              ELSE
                     DO;
                           INITIALHRS = INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS');
                           DAYHRS = INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS');
                           NEWBASEHRS=BASEHRS-INITIALHRS;
                     END;
       END;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  IF BAS_STRT_DT = POSTNG_DT THEN DO;
    INITIALHRS= ifn(days_of_po=1,BASEHRS   ,INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS'));
    DAYHRS    = ifn(days_of_po=1,INITIALHRS,INTCK('HOURS',BASSTRTDTTM,DHMS(bas_strt_dt,24,0,0),'CONTINUOUS'));
    NEWBASEHRS= ifn(days_of_po=1,INITIALHRS,BASEHRS-INITIALHRS);
  END;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it would take a little longer to run.&amp;nbsp; I wouldn't recommend it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: This comment was intended to be a response to the OP (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235176"&gt;@pink_poodle&lt;/a&gt;), but was erroneously issued as a reply to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 03:46:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828015#M327075</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-10T03:46:35Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify multiple IF blocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828017#M327076</link>
      <description>This program accrues work hours? It takes the date stamps from the log for start and finish and checks if positive hours can be computed. It also checks date time when they were posted and how that compares to start and finish.&lt;BR /&gt;</description>
      <pubDate>Wed, 10 Aug 2022 03:02:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-multiple-IF-blocks/m-p/828017#M327076</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-08-10T03:02:31Z</dc:date>
    </item>
  </channel>
</rss>

