<?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: How do I make a new variable based on values from exist columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871775#M344437</link>
    <description>&lt;P&gt;First hint is do not indent lines of data.&amp;nbsp; At best it just waste screen space and makes your program harder to read.&amp;nbsp; At worse the spaces could get into the values or through off the input statement and cause it to read the wrong locations on the line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To remind yourself (and prevent the SAS editor from auto indenting when you start a new line) do not indent the DATALINES statement either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second hint is use variable lists where they make sense to make your coding easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id type1-type6 ;
datalines;
1 1 0 0 0 0 0
2 3 1 0 0 0 0 
3 4 5 0 0 0 0
4 2 0 0 0 0 0
5 6 3 0 0 0 0 
6 7 2 1 6 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Final hints:&lt;/P&gt;
&lt;P&gt;Use&amp;nbsp; the WHICHN() function to test if a value exists.&lt;/P&gt;
&lt;P&gt;Use Boolean expressions to create 0/1 values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  groupa = whichn(1, of type1-type6) or whichn(2, of type1-type6) or whichn(3, of type1-type6);
  groupb = whichn(5, of type1-type6) or whichn(6, of type1-type6);
  groupc = 0^=whichn(4, of type1-type6);
  groupd = 0^=whichn(7, of type1-type6);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1682394919408.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83120i17C75C27E98CCEF2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1682394919408.png" alt="Tom_0-1682394919408.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 25 Apr 2023 03:55:33 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-04-25T03:55:33Z</dc:date>
    <item>
      <title>How do I make a new variable based on values from exist columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871773#M344436</link>
      <description>&lt;P&gt;My data looks like this:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data dat;
   input id type type2 type3 type4 type5 type6;
   datalines;
   1 1 0 0 0 0 0
   2 3 1 0 0 0 0 
   3 4 5 0 0 0 0
   4 2 0 0 0 0 0
   5 6 3 0 0 0 0 
   6 7 2 1 6 0 0
   ;
run;&lt;/LI-CODE&gt;&lt;P&gt;I want to create new variables based on the new classification of the type, like:&amp;nbsp;&lt;/P&gt;&lt;P&gt;group A: have either 1 or 2 or 3 in variables type-type6;&amp;nbsp;&lt;/P&gt;&lt;P&gt;group B: have either 5 or 6 in variables type-type6;&lt;/P&gt;&lt;P&gt;group C: have 4 in variables type-type6;&lt;/P&gt;&lt;P&gt;group &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt; have 7 in variables type-type6;&lt;/P&gt;&lt;P&gt;Each group is a single new column.&lt;/P&gt;&lt;P&gt;Is there a fast way to get the result I wanted?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 03:41:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871773#M344436</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2023-04-25T03:41:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do I make a new variable based on values from exist columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871775#M344437</link>
      <description>&lt;P&gt;First hint is do not indent lines of data.&amp;nbsp; At best it just waste screen space and makes your program harder to read.&amp;nbsp; At worse the spaces could get into the values or through off the input statement and cause it to read the wrong locations on the line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To remind yourself (and prevent the SAS editor from auto indenting when you start a new line) do not indent the DATALINES statement either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second hint is use variable lists where they make sense to make your coding easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id type1-type6 ;
datalines;
1 1 0 0 0 0 0
2 3 1 0 0 0 0 
3 4 5 0 0 0 0
4 2 0 0 0 0 0
5 6 3 0 0 0 0 
6 7 2 1 6 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Final hints:&lt;/P&gt;
&lt;P&gt;Use&amp;nbsp; the WHICHN() function to test if a value exists.&lt;/P&gt;
&lt;P&gt;Use Boolean expressions to create 0/1 values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  groupa = whichn(1, of type1-type6) or whichn(2, of type1-type6) or whichn(3, of type1-type6);
  groupb = whichn(5, of type1-type6) or whichn(6, of type1-type6);
  groupc = 0^=whichn(4, of type1-type6);
  groupd = 0^=whichn(7, of type1-type6);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1682394919408.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83120i17C75C27E98CCEF2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1682394919408.png" alt="Tom_0-1682394919408.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 03:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871775#M344437</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-25T03:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I make a new variable based on values from exist columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871777#M344438</link>
      <description>&lt;P&gt;Thank you for the solution! I'd like to know if I didn't change the title "type" to "type1", can I use "of type:" to indicate all the type-type6?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 04:24:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871777#M344438</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2023-04-25T04:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I make a new variable based on values from exist columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871850#M344456</link>
      <description>&lt;P&gt;You could use &lt;FONT face="courier new,courier"&gt;type:&lt;/FONT&gt; as long as there are not any other variables that start with type.&lt;/P&gt;
&lt;P&gt;Or you could use &lt;FONT face="courier new,courier"&gt;type type2-type6&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why would you want to break the naming pattern of the TYPE variables by not including the numeric suffix on the first variable?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 13:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-make-a-new-variable-based-on-values-from-exist-columns/m-p/871850#M344456</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-25T13:08:17Z</dc:date>
    </item>
  </channel>
</rss>

