<?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: Trouble with creating new variables SAS 9.4 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404491#M98324</link>
    <description>&lt;P&gt;All your IF statements execute, but they overwrite each other because of the way you've structured it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since your last line is ELSE if it doesn't match it will default everything to N/A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;	&lt;SPAN class="token keyword"&gt;IF&lt;/SPAN&gt; EggDK&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; EggColor&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'Do not know color'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
		&lt;SPAN class="token keyword"&gt;ELSE&lt;/SPAN&gt; EggColor&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'N/A'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; -&amp;gt; this is the last line and only one that works;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do the following instead and it should be slightly faster since you're no longer evaluating every IF condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		IF EggRed=1 THEN EggColor='Red';
		ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';

		ELSE IF EggOrange=1 THEN EggColor='Orange';

		
		ELSE IF EggYellow=1 THEN EggColor='Yellow';

                .... rest of your code.....

		ELSE IF EggDK=1 THEN EggColor='Do not know color';
		ELSE EggColor='N/A';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also try something different, whereby you find the variable with a 1 and get the colour from the variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _colours(*) egg: ; *list of all egg colour variables;

index_found = whichc(1, of _colours(*));
name = vname(_colours(index_found));

*then extract the colour from the name variable;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Oct 2017 15:13:44 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-10-16T15:13:44Z</dc:date>
    <item>
      <title>Trouble with creating new variables SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404483#M98322</link>
      <description>&lt;P&gt;You'll have to excuse me if this is a really basic question, but I'm trying to make a new variable composed of several other dummy coded variables. The original variables are numeric, coded 1 for yes and 0 for no. I'm trying to convert them to a new text variable that indicates their meaning so I can report descriptive statistics.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA eggs;
	set eggs;
	length EggColor $50 EggRed 5.0 EggOrange 5.0 EggYellow 5.0 EggGrn 5.0 EggBlu 5.0 EggPur 5.0 &lt;BR /&gt;        StripedEgg 5.0 EggDK 5.0 EggO 5.0;
		IF EggRed=1 THEN EggColor='Red';
		ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';

		IF EggOrange=1 THEN EggColor='Orange';
		ELSE EggColor='N/A';
		
		IF EggYellow=1 THEN EggColor='Yellow';
		ELSE EggColor='N/A';

		IF EggGrn=1 THEN EggColor='Green';
		ELSE EggColor='N/A';

		IF EggBlu=1 THEN EggColor='Blue';
		ELSE EggColor='N/A';

		IF EggPur=88 THEN EggColor='Other Color';
		ELSE EggColor='N/A';

		IF Striped=1 THEN EggColor='Striped';
		ELSE EggColor='N/A';

		IF EggDK=1 THEN EggColor='Do not know color';
		ELSE EggColor='N/A';


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is when I recode to EggsColor, all of the observations are in N/A (or '.' when I use that for my else statements) I feel like I'm missing something really basic. Is it obvious to anyone else?&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 15:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404483#M98322</guid>
      <dc:creator>TMSmith</dc:creator>
      <dc:date>2017-10-16T15:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with creating new variables SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404487#M98323</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I pesume only one of the dummy vars is 1.&amp;nbsp; Let's say eggred=1 and all the others are zero.&amp;nbsp; Then your first IF test is true, setting eggcolor to "RED".&amp;nbsp; But your subsequent IF tests are still being executed, thereby overriding the EGGCOLOR value as 'N/A';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this structure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if dummy1=1 then outcome='pink';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if dummy2=1 then outcome='black';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else outcome='N/A';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 15:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404487#M98323</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-16T15:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with creating new variables SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404491#M98324</link>
      <description>&lt;P&gt;All your IF statements execute, but they overwrite each other because of the way you've structured it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since your last line is ELSE if it doesn't match it will default everything to N/A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;	&lt;SPAN class="token keyword"&gt;IF&lt;/SPAN&gt; EggDK&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; EggColor&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'Do not know color'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
		&lt;SPAN class="token keyword"&gt;ELSE&lt;/SPAN&gt; EggColor&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'N/A'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; -&amp;gt; this is the last line and only one that works;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do the following instead and it should be slightly faster since you're no longer evaluating every IF condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		IF EggRed=1 THEN EggColor='Red';
		ELSE IF EggRed=0 OR EggRed=99 THEN EggColor='N/A';

		ELSE IF EggOrange=1 THEN EggColor='Orange';

		
		ELSE IF EggYellow=1 THEN EggColor='Yellow';

                .... rest of your code.....

		ELSE IF EggDK=1 THEN EggColor='Do not know color';
		ELSE EggColor='N/A';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also try something different, whereby you find the variable with a 1 and get the colour from the variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _colours(*) egg: ; *list of all egg colour variables;

index_found = whichc(1, of _colours(*));
name = vname(_colours(index_found));

*then extract the colour from the name variable;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Oct 2017 15:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404491#M98324</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-16T15:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with creating new variables SAS 9.4</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404506#M98325</link>
      <description>&lt;P&gt;Ahhh, okay. I didn't realize I was overwriting my code by structuring it like that. I restructured based on that and it worked. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2017 15:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-creating-new-variables-SAS-9-4/m-p/404506#M98325</guid>
      <dc:creator>TMSmith</dc:creator>
      <dc:date>2017-10-16T15:26:19Z</dc:date>
    </item>
  </channel>
</rss>

