<?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: Finding Like Values and Assigning them to a DATASET in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395162#M95248</link>
    <description>&lt;P&gt;I like the hash approach for dynamic splitting, see e.g. Dorfman et al&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf" target="_blank"&gt;http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below uses a single hash table and a DoW loop, so data must be sorted by the key. The paper also describes a Hash-of-Hashes approach if the data can't be sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Process below is:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Assign a key (first three digits of phone number or whatever you want to split by).&lt;/LI&gt;
&lt;LI&gt;Sort by the key.&lt;/LI&gt;
&lt;LI&gt;Use a DoW loop to read all data for a by-group, loading it into a hash table.&lt;/LI&gt;
&lt;LI&gt;Output the hash table to a dataset.&lt;/LI&gt;
&lt;LI&gt;Clear the hash table.&lt;/LI&gt;
&lt;LI&gt;Goto #3&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines ;
  input phonenumber $1-20 ;

  *define a key;
  mykey=substr(phonenumber,1,3);

datalines;
631 555 5555
631 666 6666
516 999 9999
516 888 8888
212 444 4444
212 444 5555
111 111 1111
999 999 9999
;
run;

proc sort data=have;
  by mykey;
run;

data _null_ ;
  if 0 then set have;
  if _n_=1 then do;
    declare hash h();
    h.definekey ("_n_") ; 
    h.definedata ("phonenumber" ) ;
    h.definedone () ;
  end;
  do _n_ = 1 by 1 until ( last.mykey ) ;
    set have ;
    by mykey ;
    h.add() ;
  end ;
  h.output (dataset: cats("OUT_",mykey) ) ;
  h.clear() ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Sep 2017 16:17:43 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2017-09-12T16:17:43Z</dc:date>
    <item>
      <title>Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395082#M95224</link>
      <description>&lt;P&gt;What is the easiest way to parse a data set and create multiple data sets based on a given value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Example:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Let’s use telephone numbers and look for all values found in PHONENUMBER beginning with&lt;/P&gt;
&lt;P&gt;631 and put them in data set A&lt;/P&gt;
&lt;P&gt;516 and put them in data set B&lt;/P&gt;
&lt;P&gt;212 and put them in data set C&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;631 555 5555&lt;/P&gt;
&lt;P&gt;631 666 6666&lt;/P&gt;
&lt;P&gt;516 999 9999&lt;/P&gt;
&lt;P&gt;516 888 8888&lt;/P&gt;
&lt;P&gt;212 444 4444&lt;/P&gt;
&lt;P&gt;212 444 5555&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Problem:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You don’t know the first 3 values – If I did – I would write a substring/do loop to read the first 3 of the field “PHONENUMBER” and tell it to read through the table and when it finds a matching value “substr (phonenumber,1,3) write it out to the associated table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What function would you use to search the field PHONENUMBER and look for all like values from a specific position and place those values in a separate data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is &lt;STRONG&gt;without&lt;/STRONG&gt; defining the values to look for since the list of area codes is quite large.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All help is greatly appreciated – I haven’t been in the SAS world for over 2 years so I’m a little rusty – Thanks in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 13:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395082#M95224</guid>
      <dc:creator>Dsrountree</dc:creator>
      <dc:date>2017-09-12T13:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395091#M95227</link>
      <description>&lt;P&gt;The most elegant way would be to use hashing.&amp;nbsp; Since I'm out of my element there, here's a link to a similar question ... refer to the final suggested solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/create-a-new-Dataset-for-every-unique-Country-and-it-should-have/m-p/394342#M95002" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/create-a-new-Dataset-for-every-unique-Country-and-it-should-have/m-p/394342#M95002&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your situation may be mildly more complex, since the area code itself would not be a valid name for a SAS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It could be done without hashing, using macro language to extract all the area codes, place a set of names on the DATA statement, and construct a series of IF/THEN statements to control outputting.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 13:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395091#M95227</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-12T13:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395107#M95230</link>
      <description>&lt;P&gt;If I was only looking at a distinct value like zipcode then I could use code like that.&lt;/P&gt;
&lt;P&gt;The problem is I need to evalute the value based on a given position and separate accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions...&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 13:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395107#M95230</guid>
      <dc:creator>Dsrountree</dc:creator>
      <dc:date>2017-09-12T13:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395112#M95231</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I won't use hash for this, since the assignment is not known. This is not a classical look up situation.&lt;/P&gt;&lt;P&gt;Instead I would just program where to output the rows and use a simple call execute to perform the job:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
infile datalines ;
input phonenumber $1-20 @;
datalines;
631 555 5555
631 666 6666
516 999 9999
516 888 8888
212 444 4444
212 444 5555
;
run;

data test1;
   set test;
   first3=substr(phonenumber,1,3);
run;
proc sort data=test1;
   by first3 phonenumber;
run;

data test2;
   set test1;
   length ds $10;
   by first3;
   retain count .;
   if first.first3 then count+1;
   if count&amp;gt;26 then ds=repeat('a',int(count/26));
   ds=strip(ds)||byte(96+count);  
run;

data _NULL_;
   set test2;
   by first3;
   if first.first3;
   call execute('data '||strip(ds)||'(drop=first3 count ds); set test2; where ds eq "'||strip(ds)||'"; run;');
run;


&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 14:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395112#M95231</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-09-12T14:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395114#M95233</link>
      <description>&lt;P&gt;You'll have to cross that bridge, no matter what the approach.&amp;nbsp; It might be easier to plan:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if the phone number begins with 631, put it into data set named _631&lt;/P&gt;
&lt;P&gt;if the phone number begins with 516, put it into data set named _516&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if using hashing, you might have to construct the key to the hash table, concatenating an underscore and the area code (i think again out of my element here).&amp;nbsp; But if you were using macro language, you would need to do something similar to get the names of the data sets, for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;select distinct '_' || substr(phone, 1, 3) into : data_set_list separated by ' ' from have;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the macro language approach would use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data &amp;amp;data_set_list;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 14:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395114#M95233</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-12T14:13:24Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395145#M95239</link>
      <description>&lt;P&gt;Nicely done....&lt;/P&gt;
&lt;P&gt;But quick question on data _NULL_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I have several other columns,&lt;/P&gt;
&lt;P&gt;IE: First Name, Last Name, Address.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will that information also carry over to table A, B, C?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 15:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395145#M95239</guid>
      <dc:creator>Dsrountree</dc:creator>
      <dc:date>2017-09-12T15:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395153#M95244</link>
      <description>&lt;P&gt;Looks like nobody is stepping up with a hashing solution.&amp;nbsp; Here's how I would approach a CALL EXECUTE solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc freq data=have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; tables phone_number / noprint out=counts (keep=phone_number);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; format phone_number $3.;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data _null_;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; length phone_number $ 3;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; call execute ('data ');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; do until (done1);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set counts end=done1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call execute('_' || phone_number);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;call execute('; set have; select (substr(phone_number, 1, 3));');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; do until (done2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set counts end=done2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call execute('when ("' || phone_number || '") output _' || phone_number || ';');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; call execute('end; run;');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;stop;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;It's untested, but looks like a viable approach.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 15:39:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395153#M95244</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-12T15:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395154#M95245</link>
      <description>&lt;P&gt;sure, it's just droping the unwanted variables, the other variables remain where they are.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 15:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395154#M95245</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-09-12T15:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Like Values and Assigning them to a DATASET</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395162#M95248</link>
      <description>&lt;P&gt;I like the hash approach for dynamic splitting, see e.g. Dorfman et al&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf" target="_blank"&gt;http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below uses a single hash table and a DoW loop, so data must be sorted by the key. The paper also describes a Hash-of-Hashes approach if the data can't be sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Process below is:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Assign a key (first three digits of phone number or whatever you want to split by).&lt;/LI&gt;
&lt;LI&gt;Sort by the key.&lt;/LI&gt;
&lt;LI&gt;Use a DoW loop to read all data for a by-group, loading it into a hash table.&lt;/LI&gt;
&lt;LI&gt;Output the hash table to a dataset.&lt;/LI&gt;
&lt;LI&gt;Clear the hash table.&lt;/LI&gt;
&lt;LI&gt;Goto #3&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines ;
  input phonenumber $1-20 ;

  *define a key;
  mykey=substr(phonenumber,1,3);

datalines;
631 555 5555
631 666 6666
516 999 9999
516 888 8888
212 444 4444
212 444 5555
111 111 1111
999 999 9999
;
run;

proc sort data=have;
  by mykey;
run;

data _null_ ;
  if 0 then set have;
  if _n_=1 then do;
    declare hash h();
    h.definekey ("_n_") ; 
    h.definedata ("phonenumber" ) ;
    h.definedone () ;
  end;
  do _n_ = 1 by 1 until ( last.mykey ) ;
    set have ;
    by mykey ;
    h.add() ;
  end ;
  h.output (dataset: cats("OUT_",mykey) ) ;
  h.clear() ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Sep 2017 16:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Like-Values-and-Assigning-them-to-a-DATASET/m-p/395162#M95248</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-09-12T16:17:43Z</dc:date>
    </item>
  </channel>
</rss>

