<?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: New variable length in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874310#M345421</link>
    <description>&lt;P&gt;Because in the&amp;nbsp;&lt;EM&gt;first occurence&lt;/EM&gt; in your code you give it a length of 1:&lt;/P&gt;
&lt;PRE&gt;IF _n_&amp;lt;=2 then &lt;FONT color="#FF0000"&gt;X_new=''&lt;/FONT&gt;;else X_new=X;&lt;/PRE&gt;
&lt;P&gt;The data step compiler takes the length from this first occurence to define the variable in the PDV.&lt;/P&gt;
&lt;P&gt;Bottom line: to set a length for a variable, always use one of the explicit methods (LENGTH or ATTRIB statement) instead of implicit methods like you did here.&lt;/P&gt;</description>
    <pubDate>Sun, 07 May 2023 04:54:56 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-05-07T04:54:56Z</dc:date>
    <item>
      <title>New variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874308#M345420</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the reason that in data set "Have2"&amp;nbsp; the new variable X_new have length 1 and not 3 as in the original variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X $;
cards;
MAX
MAX
VISA
DINERS
MAX
;
Run;

data have2 ;
set have;
IF _n_&amp;lt;=2 then X_new='';else X_new=X;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 May 2023 04:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874308#M345420</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-07T04:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: New variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874310#M345421</link>
      <description>&lt;P&gt;Because in the&amp;nbsp;&lt;EM&gt;first occurence&lt;/EM&gt; in your code you give it a length of 1:&lt;/P&gt;
&lt;PRE&gt;IF _n_&amp;lt;=2 then &lt;FONT color="#FF0000"&gt;X_new=''&lt;/FONT&gt;;else X_new=X;&lt;/PRE&gt;
&lt;P&gt;The data step compiler takes the length from this first occurence to define the variable in the PDV.&lt;/P&gt;
&lt;P&gt;Bottom line: to set a length for a variable, always use one of the explicit methods (LENGTH or ATTRIB statement) instead of implicit methods like you did here.&lt;/P&gt;</description>
      <pubDate>Sun, 07 May 2023 04:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874310#M345421</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-07T04:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: New variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874355#M345450</link>
      <description>So better always to define length of a new variable and not let SAS decide for me....</description>
      <pubDate>Sun, 07 May 2023 20:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874355#M345450</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-07T20:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: New variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874357#M345451</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So better always to define length of a new variable and not let SAS decide for me....&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you KNOW the length it should have then yes be explicit.&amp;nbsp; The programmer that has to fix your code (which is usually you) will appreciate it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make the code mimic an existing variable then you could easily re-write your logic so the compiler sees the simple copy of the old variable as the first place the new variable is referenced.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2 ;
  set have;
  X_new=X;
  if _n_&amp;lt;=2 then X_new=' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you use CALL MISSING() it will work whether X is numeric or character.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2 ;
  set have;
  X_new=X;
  if _n_&amp;lt;=2 then call missing(X_new);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want it to inherit the format, label and other attributes then you could get even trickier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2 ;
  set have;
  set have(keep=x rename=(x=X_new));
  if _n_&amp;lt;=2 then call missing(X_new);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 May 2023 20:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874357#M345451</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-07T20:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: New variable length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874380#M345468</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So better always to define length of a new variable and not let SAS decide for me....&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;aka Maxim 47.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 03:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-length/m-p/874380#M345468</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-08T03:40:19Z</dc:date>
    </item>
  </channel>
</rss>

