<?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: Values offset by one row upon new variable creation and conditional value assignment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744361#M233200</link>
    <description>&lt;P&gt;Move the line that says &lt;FONT face="courier new,courier"&gt;set input;&lt;/FONT&gt; to immediately below the &lt;FONT face="courier new,courier"&gt;length&lt;/FONT&gt; statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since SAS dates are numeric, you do not want to create a character variable YEARBUILT containing 01/01/1970. You should really make YEARBUILT into a numeric variable, as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if CNS_P_LBL = "1971-1990" then  YEARBUILT = '01JAN1971'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you should assign YEARBUILT the format DATE9. or any other date format you like.&lt;/P&gt;</description>
    <pubDate>Fri, 28 May 2021 10:46:46 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-05-28T10:46:46Z</dc:date>
    <item>
      <title>Values offset by one row upon new variable creation and conditional value assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744357#M233199</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm running into a strange issue. I'm trying to assign values to the variable YEARBUILT based on the value of CNS_P_LBL like so:&lt;/P&gt;&lt;PRE&gt;data OUTPUT;
length YEARBUILT $100;
format YEARBUILT $100.;
informat YEARBUILT $100.;
length CNS_P_LBL $16;
if CNS_P_LBL = "1971-1990" then  YEARBUILT = "01/01/1971";
if CNS_P_LBL = "1991-2000" then  YEARBUILT = "01/01/1990";
if CNS_P_LBL = "2001-2005" then  YEARBUILT = "01/01/2003";
if CNS_P_LBL = "2006-2010" then  YEARBUILT = "01/01/2008";
if CNS_P_LBL = "2011-2015" then  YEARBUILT = "01/01/2013";
if CNS_P_LBL = "2016-2020" then  YEARBUILT = "01/01/2018";
if CNS_P_LBL = "1970 or before" then  YEARBUILT = "12/31/9999"; 
set INPUT;&lt;/PRE&gt;&lt;P&gt;However, this results in shifted YEARBUILT values by one row. It is shown in the picture below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image.png" style="width: 237px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59832i6EFE5F74BE231CD6/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 10:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744357#M233199</guid>
      <dc:creator>YD5265</dc:creator>
      <dc:date>2021-05-28T10:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Values offset by one row upon new variable creation and conditional value assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744361#M233200</link>
      <description>&lt;P&gt;Move the line that says &lt;FONT face="courier new,courier"&gt;set input;&lt;/FONT&gt; to immediately below the &lt;FONT face="courier new,courier"&gt;length&lt;/FONT&gt; statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since SAS dates are numeric, you do not want to create a character variable YEARBUILT containing 01/01/1970. You should really make YEARBUILT into a numeric variable, as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if CNS_P_LBL = "1971-1990" then  YEARBUILT = '01JAN1971'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you should assign YEARBUILT the format DATE9. or any other date format you like.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 10:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744361#M233200</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-28T10:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Values offset by one row upon new variable creation and conditional value assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744400#M233220</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/383642"&gt;@YD5265&lt;/a&gt;&amp;nbsp;Adding an explanation to why you got the results you did, prior to implementing&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;solution.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;There are two types of Data Step statements, &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p10bvg3wauedhan1qly0hiokirlv.htm" target="_self"&gt;Declarative &amp;amp; Executable&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;The first 5 lines (up to and including length CNS_P_LBL $16;) are all declarative, the rest executable.&lt;/LI&gt;
&lt;LI&gt;Executable statements are processed in the order they appear in the data step.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Your first executable statement is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if CNS_P_LBL = "1971-1990" then  YEARBUILT = "01/01/1971";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At this point in the data step execution CNS_P_LBL (as is YEARBUILT) is an empty string, it is a known variable as it was declared in the length statement during the compilation phase.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of your IF statements are true when CNS_P_LBL is an empty string so YEARBUILT will be an empty string.&amp;nbsp;&lt;BR /&gt;When the execution phase reaches the set INPUT; statement, then CNS_P_LBL is read from the INPUT data set, resulting in its value being set to 1971-1990.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;An easy way to see this to understand it, is to use PUT statements within your code (note this is a handy debugging method):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data OUTPUT;
length YEARBUILT $100;
format YEARBUILT $100.;
informat YEARBUILT $100.;
length CNS_P_LBL $16;
PUT "1) " _all_ ; /* _all_ will output all the variable values */ 
if CNS_P_LBL = "1971-1990" then  YEARBUILT = "01/01/1971";
if CNS_P_LBL = "1991-2000" then  YEARBUILT = "01/01/1990";
if CNS_P_LBL = "2001-2005" then  YEARBUILT = "01/01/2003";
if CNS_P_LBL = "2006-2010" then  YEARBUILT = "01/01/2008";
if CNS_P_LBL = "2011-2015" then  YEARBUILT = "01/01/2013";
if CNS_P_LBL = "2016-2020" then  YEARBUILT = "01/01/2018";
if CNS_P_LBL = "1970 or before" then  YEARBUILT = "12/31/9999"; 
PUT "2) " _all_ ; /* _all_ will output all the variable values */ 
set INPUT;
PUT "3) " _all_ ; /* _all_ will output all the variable values */ 
RUN ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Paige's solution works, because once you move the SET statement prior to the IF statements, then the variable CNS_P_LBL gets assigned prior to the IF statements.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 13:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Values-offset-by-one-row-upon-new-variable-creation-and/m-p/744400#M233220</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-05-28T13:59:41Z</dc:date>
    </item>
  </channel>
</rss>

