<?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 how to assign 2 different values of a new variable according to the values of an existing variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463812#M285018</link>
    <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;I tried to assign 2 different values (1, 0) of a new variable&amp;nbsp;according to the value of an existing variable. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target in (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"rain"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"snow"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target_kgb = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"1"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"sunny"&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target_kgb = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"0"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;But&amp;nbsp;I can only assign 1 to target_kgb if target in rain or snow. 0 was not assigned to target_kgb even though there were many "sunny" for target. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Any help is highly appreciated. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thank a lot. &lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 21 May 2018 16:22:04 GMT</pubDate>
    <dc:creator>jeniffer_jacob</dc:creator>
    <dc:date>2018-05-21T16:22:04Z</dc:date>
    <item>
      <title>how to assign 2 different values of a new variable according to the values of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463812#M285018</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;I tried to assign 2 different values (1, 0) of a new variable&amp;nbsp;according to the value of an existing variable. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test2;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target in (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"rain"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"snow"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target_kgb = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"1"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"sunny"&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; target_kgb = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"0"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;But&amp;nbsp;I can only assign 1 to target_kgb if target in rain or snow. 0 was not assigned to target_kgb even though there were many "sunny" for target. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Any help is highly appreciated. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thank a lot. &lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 16:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463812#M285018</guid>
      <dc:creator>jeniffer_jacob</dc:creator>
      <dc:date>2018-05-21T16:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign 2 different values of a new variable according to the values of an existing variab</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463817#M285019</link>
      <description>&lt;P&gt;Check the length of your variable "target". Without anything else it will be defined as $4 because "rain" is the first possible value the datastep finds. it is safer to add a statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length target $5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;before the if-then-else statements. This will prevent "sunny" to be truncated to "sunn" which never matches "sunny". Possibly this is already happening in the creation of dataset test. We would need to see more of your program and logs to know for sure. Do a proc contents on dataset test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;-- Jan&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 16:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463817#M285019</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2018-05-21T16:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign 2 different values of a new variable according to the values of an existing variab</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463818#M285020</link>
      <description>&lt;P&gt;Your code is perfect.&amp;nbsp; Most likely, the data are a little bit different.&amp;nbsp; For example, none of these would produce a match with "sunny":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sunny&lt;/P&gt;
&lt;P&gt;s u n n y&lt;/P&gt;
&lt;P&gt;suny&lt;/P&gt;
&lt;P&gt;SUNNY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sunny&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Character strings must match exactly.&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 16:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463818#M285020</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-21T16:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign 2 different values of a new variable according to the values of an existing variab</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463819#M285021</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your data step code worked OK for me when I tried the following data:&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 test;
   input target $8.;
   datalines;
rain
snow
sunny
;

data test2;
   set test;
   if target in ("rain", "snow") then target_kgb = "1";
   else if target = "sunny" then target_kgb = "0";
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;Perhaps you have a leading space(s) before "sunny" in the variable target, in which case you could try using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if left(target) = "sunny" then target_kgb = "0";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that works then I recommend you use &lt;FONT face="courier new,courier"&gt;left(target)&lt;/FONT&gt; wherever you have &lt;FONT face="courier new,courier"&gt;target&lt;/FONT&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 16:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463819#M285021</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-05-21T16:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign 2 different values of a new variable according to the values of an existing variab</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463832#M285022</link>
      <description>&lt;P&gt;"sunny" is actually "Sunny".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks to all!&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 17:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-2-different-values-of-a-new-variable-according-to/m-p/463832#M285022</guid>
      <dc:creator>jeniffer_jacob</dc:creator>
      <dc:date>2018-05-21T17:38:12Z</dc:date>
    </item>
  </channel>
</rss>

