<?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: SAS select function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398305#M96332</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;, you meant&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length &lt;FONT color="#ff0000"&gt;test&lt;/FONT&gt; $1 name $5;&lt;/P&gt;</description>
    <pubDate>Sat, 23 Sep 2017 19:08:03 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2017-09-23T19:08:03Z</dc:date>
    <item>
      <title>SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398276#M96320</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data temp;&lt;/P&gt;&lt;P&gt;test = 'X';&lt;/P&gt;&lt;P&gt;select(test);&lt;/P&gt;&lt;P&gt;when('Y') Name = 'Ste';&lt;/P&gt;&lt;P&gt;when('X') Name = 'Steve';&lt;/P&gt;&lt;P&gt;when('Z') Name = ' ';&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;What is the output?&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;A)&amp;nbsp;Steve;&lt;/LI&gt;&lt;LI&gt;B)&amp;nbsp;Ste&lt;/LI&gt;&lt;LI&gt;C)&amp;nbsp;Missing (character missing value)&lt;/LI&gt;&lt;LI&gt;D)&amp;nbsp;SYNTAX ERROR&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The answer is B which I do not understand, Isn't only X fulfiled the criteria so it should output "Steve"? Thanks so much for helping!&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 08:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398276#M96320</guid>
      <dc:creator>pchen002</dc:creator>
      <dc:date>2017-09-23T08:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398277#M96321</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/163028"&gt;@pchen002&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;That's a "mean" question - but a good one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are absolutely correct that the 2nd condition &lt;SPAN&gt;&lt;EM&gt;when('X') Name = 'Steve';&lt;/EM&gt;&amp;nbsp;is the one which gets&lt;/SPAN&gt;&amp;nbsp;TRUE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually execute the data step you've posted you'll still see that in the result table &lt;EM&gt;temp&lt;/EM&gt; variable &lt;EM&gt;Name&lt;/EM&gt; will have a value of &lt;EM&gt;Ste&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason for this is that the first time you're using variable &lt;EM&gt;Name&lt;/EM&gt; in your data step it gets a string with 3 characters assigned.&lt;/P&gt;
&lt;P&gt;Think of each data step having an iteration zero where the SAS compiler investigates the data step and creates all necessary variables. It's with the SAS compiler first comes first and for Name it finds a string assignment with 3 characters and though creates the variable &lt;EM&gt;Name&lt;/EM&gt; only with a length of 3 characters.&lt;/P&gt;
&lt;P&gt;When SAS then iterates through the observations (iteration one to n) then your 2nd condition becomes TRUE and SAS assigns &lt;EM&gt;Steve&lt;/EM&gt; to &lt;EM&gt;Name.&amp;nbsp;&lt;/EM&gt;...but as variable&amp;nbsp;&lt;EM&gt;Name&lt;/EM&gt; has only a length of 3 the string gets truncated and you end up with &lt;EM&gt;Ste&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What this sample teaches you: Define variable lengths for such cases explicitly at the beginning of your data step.&lt;/P&gt;
&lt;P&gt;data temp;&lt;BR /&gt; &lt;STRONG&gt;length test $1 name $5;&lt;/STRONG&gt;&lt;BR /&gt; test = 'X';&lt;BR /&gt; select(test);&lt;BR /&gt; when('Y') Name = 'Ste';&lt;BR /&gt; when('X') Name = 'Steve';&lt;BR /&gt; when('Z') Name = ' ';&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Update: Code fixed as per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;post.&lt;/P&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>Sat, 23 Sep 2017 23:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398277#M96321</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-23T23:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398305#M96332</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;, you meant&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length &lt;FONT color="#ff0000"&gt;test&lt;/FONT&gt; $1 name $5;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 19:08:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398305#M96332</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-09-23T19:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398342#M96347</link>
      <description>&lt;P&gt;Ya, I do not quite understand too, how do you know "&lt;SPAN&gt;&amp;nbsp;variable&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;Name&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;in your data step it gets a string with 3 characters assigned"?? Please guide, thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 02:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398342#M96347</guid>
      <dc:creator>pchen002</dc:creator>
      <dc:date>2017-09-24T02:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398343#M96348</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/163028"&gt;@pchen002&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The variable NAME in your data step gets created with a length of 3 so it can only hold 3 characters. That's too short for Steve (=5 characters). The string will get truncated to 3 characters (Ste).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How the SAS data step compilation phase works and how the PDV gets created are important basics for you to learn and understand.&lt;/P&gt;
&lt;P&gt;The SAS Language Reference: Concepts manual covers a lot of these topics and it's documentation you should read and try to understand.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg3xqwfxful.htm&amp;nbsp;" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg3xqwfxful.htm&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 06:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398343#M96348</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-24T06:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398345#M96349</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/163028"&gt;@pchen002&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Ya, I do not quite understand too, how do you know "&lt;SPAN&gt;&amp;nbsp;variable&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;Name&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;in your data step it gets a string with 3 characters assigned"?? Please guide, thanks!&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Generally SAS will define a variable when it first encounters when compiling the data step. In you example since there are no input datesets that could ahve contained the NAME variable the first place that NAME is mentioned is in this assignment statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Name = 'Ste';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you are setting NAME to a string constant SAS will define NAME as a character with the length of the constant that you are setting it to.&lt;/P&gt;
&lt;P&gt;Depending on what order you placed your three WHEN statements you could have made NAME as length $5 (like below) or length $1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
test = 'X';
select(test);
when('X') Name = 'Steve';
when('Y') Name = 'Ste';
when('Z') Name = ' ';
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Sep 2017 02:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398345#M96349</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-24T02:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS select function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398351#M96350</link>
      <description>&lt;P&gt;Thanks!!Understood!&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 04:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-select-function/m-p/398351#M96350</guid>
      <dc:creator>pchen002</dc:creator>
      <dc:date>2017-09-24T04:01:02Z</dc:date>
    </item>
  </channel>
</rss>

