<?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: Split a table based on condition of another table variable values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820728#M323989</link>
    <description>You would need to load critter and type into a hash and use that as a reference (i.e., look-up table). Here is an example:&lt;BR /&gt;&lt;A href="https://support.sas.com/kb/47/224.html#:~:text=Load%20a%20SAS%20data%20set%20into%20a%20Hash,step%20to%20look%20up%20values%20in%20a%20table" target="_blank"&gt;https://support.sas.com/kb/47/224.html#:~:text=Load%20a%20SAS%20data%20set%20into%20a%20Hash,step%20to%20look%20up%20values%20in%20a%20table&lt;/A&gt;.</description>
    <pubDate>Tue, 28 Jun 2022 16:39:52 GMT</pubDate>
    <dc:creator>pink_poodle</dc:creator>
    <dc:date>2022-06-28T16:39:52Z</dc:date>
    <item>
      <title>Split a table based on condition of another table variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820717#M323979</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets A and B.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset A&lt;/STRONG&gt; with the following variable names&lt;/P&gt;&lt;PRE&gt;Name | Area |   Code   |  Num1  |  Num2  | Num3   | Num4   | Num5 |   Num6 |   Num7 |
AfGi    NY      A2134    34,57    45,67   89,00     43,78    56,76    23,45    53,23
LmKo    SR      G3897    94,36    23,89   73,23     26,87    12,20    34,50    78,98
  ..           ..       ....
 ..           ..        ....&lt;/PRE&gt;&lt;P&gt;where Name, Area and Code are Character variables&lt;BR /&gt;and Num1, Num2, Num3, Num4, Num5, Num6, Num7 are Numeric variables&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset B&lt;/STRONG&gt; has two character variables with names Criter and Type and looks like:&lt;/P&gt;&lt;PRE&gt; Criter    Type
  Num1     RED
  Num2     RED
  Num3     RED
  Num4     GREEN
  Num5     GREEN
  Num6     GREEN
  Num7     GREEN&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need &lt;STRONG&gt;to split table A into two Tables&lt;/STRONG&gt;&lt;BR /&gt;A_RED and&lt;BR /&gt;A_GREEN&lt;BR /&gt;based on the combination of Criter and Type values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Table A_RED

Name | Area |   Code   |  Num1  |  Num2  | Num3    |  Type
AfGi    NY      A2134     34,57    45,67   89,00      RED
LmKo    SR      G3897     94,36    23,89   73,23      RED 
..    ...    ...
and 
Table a_RED

Name | Area |   Code   |  Num4   |  Num5  |  Num6  |   Num7 |   Type
AfGi    NY      A2134     43,78     56,76    23,45    53,23     GREEN
LmKo    SR      G3897     26,87     12,20    34,50    78,98     GREEN&lt;BR /&gt;..    ...    ....&lt;/PRE&gt;&lt;P&gt;I would very much appreciate your guidance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 15:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820717#M323979</guid>
      <dc:creator>Zeus_Olympus</dc:creator>
      <dc:date>2022-06-28T15:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Split a table based on condition of another table variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820728#M323989</link>
      <description>You would need to load critter and type into a hash and use that as a reference (i.e., look-up table). Here is an example:&lt;BR /&gt;&lt;A href="https://support.sas.com/kb/47/224.html#:~:text=Load%20a%20SAS%20data%20set%20into%20a%20Hash,step%20to%20look%20up%20values%20in%20a%20table" target="_blank"&gt;https://support.sas.com/kb/47/224.html#:~:text=Load%20a%20SAS%20data%20set%20into%20a%20Hash,step%20to%20look%20up%20values%20in%20a%20table&lt;/A&gt;.</description>
      <pubDate>Tue, 28 Jun 2022 16:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820728#M323989</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-06-28T16:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: Split a table based on condition of another table variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820739#M323995</link>
      <description>&lt;P&gt;Sounds like you want to generate code from your B dataset.&lt;/P&gt;
&lt;P&gt;First let's make some sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b;
  input varname :$32. Type :$20.;
cards;
Num1 RED
Num2 RED
Num3 RED
Num4 GREEN
Num5 GREEN
Num6 GREEN
Num7 GREEN
;

data a;
  input Name $ Area $  Code $ num1-num7;
cards;
AfGi NY A2134 34.57 45.67 89.00 43.78 56.76 23.45 53.23
LmKo SR G3897 94.36 23.89 73.23 26.87 12.20 34.50 78.98
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's use B to generate some code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=b;
  by type varname;
run;

filename code temp;
data _null_;
  file code ;
  set b end=eof;
  by type;
  if _n_=1 then put 'data';
  if first.type then put ' a_' type '(keep=name area code ' type @;
  put varname @;
  if last.type then put 'rename=(' type '=type) where=(' type= $quote. '))' ;
  if eof then put ';' ;
run;

data _null_;
  file code mod;
  set b end=eof;
  by type;
  if first.type then put 'length ' type '$20.;' type '=' type :$quote. ';' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's use that code. We can just add a SET statement to tell where the data is coming from.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include code / source ;
  set a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS Log&lt;/P&gt;
&lt;PRE&gt;856  %include code / source ;
NOTE: %INCLUDE (level 1) file CODE is file C:\Users\...\#LN00152.
857 +data
858 + a_GREEN (keep=name area code GREEN Num4 Num5 Num6 Num7 rename=(GREEN =type) where=(Type="GREEN" ))
859 + a_RED (keep=name area code RED Num1 Num2 Num3 rename=(RED =type) where=(Type="RED" ))
860 +;
861 +length GREEN $20.;GREEN ="GREEN" ;
862 +length RED $20.;RED ="RED" ;
NOTE: %INCLUDE (level 1) ending.
863    set a;
864  run;

NOTE: There were 2 observations read from the data set WORK.A.
NOTE: The data set WORK.A_GREEN has 2 observations and 8 variables.
NOTE: Compressing data set WORK.A_GREEN increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: The data set WORK.A_RED has 2 observations and 7 variables.
NOTE: Compressing data set WORK.A_RED increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&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-1656435800752.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72781iDEC056A266FAED9C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1656435800752.png" alt="Tom_0-1656435800752.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 17:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820739#M323995</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-28T17:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Split a table based on condition of another table variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820903#M324058</link>
      <description>Thanks, Tom!</description>
      <pubDate>Wed, 29 Jun 2022 13:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-table-based-on-condition-of-another-table-variable/m-p/820903#M324058</guid>
      <dc:creator>Zeus_Olympus</dc:creator>
      <dc:date>2022-06-29T13:25:14Z</dc:date>
    </item>
  </channel>
</rss>

