<?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: Merging Issue in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281126#M56944</link>
    <description>Awesome work man.!! Kudos. It works.</description>
    <pubDate>Wed, 29 Jun 2016 15:04:47 GMT</pubDate>
    <dc:creator>wizkid2050</dc:creator>
    <dc:date>2016-06-29T15:04:47Z</dc:date>
    <item>
      <title>Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/280992#M56893</link>
      <description>&lt;P&gt;data test;&lt;BR /&gt;input accounts;&lt;BR /&gt;datalines;&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;5&lt;BR /&gt;6&lt;BR /&gt;7&lt;BR /&gt;8&lt;BR /&gt;9&lt;BR /&gt;10&lt;BR /&gt;11&lt;BR /&gt;12&lt;BR /&gt;13&lt;BR /&gt;14&lt;BR /&gt;15&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;input managers$ count;&lt;BR /&gt;datalines;&lt;BR /&gt;a 48&lt;BR /&gt;b 45&lt;BR /&gt;c 49&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have these two data sets i want an output of&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 a&lt;/P&gt;&lt;P&gt;2 b&lt;/P&gt;&lt;P&gt;3 c&lt;/P&gt;&lt;P&gt;4 a&lt;/P&gt;&lt;P&gt;5 b&lt;/P&gt;&lt;P&gt;6 b&lt;/P&gt;&lt;P&gt;7 b&lt;/P&gt;&lt;P&gt;8 b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after every merge step the value for count should increase for each variable and if the count reaches 50 then&lt;/P&gt;&lt;P&gt;it should not merge for that step.&lt;/P&gt;&lt;P&gt;plz help me with any logic if possible.&lt;/P&gt;&lt;P&gt;Free to use macros or sql.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 08:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/280992#M56893</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T08:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281002#M56897</link>
      <description>&lt;P&gt;Looks like the perfect example for using a hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=accounts managers);
set test;
length managers $8 count 8;
retain managers;
if _n_ = 1
then do;
  declare hash test2(dataset:'test2', ordered: 'yes');
  declare hiter iter('test2');
  test2.definekey('managers');
  test2.definedata('managers','count');
  test2.definedone();
  call missing(managers,count);
  iter.first();
end;
put 'Start of step';
put managers=;
put count=;
runover = 0;
do while (count &amp;gt;= 50);
  put 'while iteration';
  rc = iter.next();
put managers=;
put count=;
  if rc ne 0
  then do;
    if runover then leave;
    runover = 1;
    rc = iter.first();
  end;
end;
if runover and rc
then do;
  put 'No managers left';
  stop;
end;
put 'after while iteration';
put managers=;
put count=;
count + 1;
test2.replace();
output;
rc = iter.next();
if rc then rc = iter.first();
put 'end of step';
put managers=;
put count=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the many PUTs in there are for debugging and clarification how it works.&lt;/P&gt;
&lt;P&gt;The RETAIN for managers is necessary, or the key value will be set to missing at the start of each data step iteration.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 08:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281002#M56897</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-29T08:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281003#M56898</link>
      <description>Can't it be done in more simplified manner than this?&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Jun 2016 08:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281003#M56898</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T08:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281005#M56900</link>
      <description>&lt;P&gt;Well, your problem is not a simple one, so don't expect simple solutions.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 08:59:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281005#M56900</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-29T08:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281007#M56901</link>
      <description>&lt;P&gt;Addendum:&lt;/P&gt;
&lt;P&gt;if it were simple, you wouldn't have needed to come here.&lt;/P&gt;
&lt;P&gt;And you just have a fine opportunity to learn about one of the more arcane, but very useful features of the SAS data step language (hash objects).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281007#M56901</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-29T09:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281009#M56902</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this should work :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc transpose data=test2 out=tr_test2;
id managers;
idlabel managers;
run;

	data want;
		set tr_test2 (drop=_NAME_);
		array a_mngrs {*}_NUMERIC_;
		
		if _N_=1 then index=1;

		do accounts=1 to 15;
			if a_mngrs{index} lt 50 then do;
				put index=;
				a_mngrs{index}=a_mngrs{index}+1;
				managers=vname(a_mngrs{index});
				output;
			end;
			
			/* Probably a better way to do this */
			index=mod(index+1,3);
			if index=0 then index=3;
		end;
	run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281009#M56902</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T09:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281011#M56903</link>
      <description>&lt;P&gt;Thanks for the Solution Sir.&lt;/P&gt;&lt;P&gt;Could you also point me in the direction regarding what and where to read about hash objects so that i could understand the code better.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281011#M56903</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T09:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281013#M56904</link>
      <description>&lt;P&gt;It doesn't, sorry &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281013#M56904</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T09:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281014#M56905</link>
      <description>&lt;P&gt;The code seems to work correctly.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281014#M56905</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T09:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281016#M56906</link>
      <description>&lt;P&gt;There was a proble with the accounts column. Here is a corrected version :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	data want;
		set tr_test2 (drop=_NAME_);
		array a_mngrs {*}_NUMERIC_;
		keep accounts managers;
		
		if _N_=1 then index=1;

		accounts=0;

		do i=1 to 15;
			if a_mngrs{index} lt 50 then do;
				a_mngrs{index}=a_mngrs{index}+1;
				managers=vname(a_mngrs{index});
				accounts+1;
				output;
			end;
			
			/* Probably a better way to do this */
			index=mod(index+1,dim(a_mngrs));
			if index=0 then index=dim(a_mngrs);
		end;
	run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281016#M56906</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T09:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281017#M56907</link>
      <description>&lt;P&gt;The code seems to run fine but the issue here is you are not making use of first dataset named test.&lt;/P&gt;&lt;P&gt;although i have 1 to 15 there but what if i have that i some random order.&lt;/P&gt;&lt;P&gt;and i dont want to sort it. it has to be merged as it is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281017#M56907</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T09:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281020#M56908</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80369"&gt;@wizkid2050&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the Solution Sir.&lt;/P&gt;
&lt;P&gt;Could you also point me in the direction regarding what and where to read about hash objects so that i could understand the code better.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A very good (and quite extensive) paper can be found here:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi31/241-31.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi31/241-31.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This is the relevant part of the Data Step Concepts part of the SAS documentation:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002586295.htm" target="_blank"&gt;Using DATA Step Component Objects&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This is the reference for the language elements (statements and methods):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003143739.htm" target="_blank"&gt;Hash and Hash Iterator Object Language Elements&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can find all this, and more, with a google search for "sas hash objects basics"&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281020#M56908</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-29T09:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281023#M56910</link>
      <description>&lt;P&gt;To help you a little in understanding what happens, I've added some comments:&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=accounts managers);
set test;
length managers $8 count 8;
* necessary so we don't get into trouble because of wrong variable types;
retain managers;
* because of the retain statement, the key of the hash object
  is kept from one data step iteration to the next;
if _n_ = 1
then do; * set up the hash object in the very first iteration of the data step;
  * declare the hash;
  declare hash test2(dataset:'test2', ordered: 'yes');
  * from the hash, declare the iterative hash object for sequential search;
  declare hiter iter('test2');
  * define hash object members;
  test2.definekey('managers');
  test2.definedata('managers','count');
  test2.definedone();
  * set to missing to avoid notes;
  call missing(managers,count);
  * intialize;
  iter.first();
end;
runover = 0; * this will flag if we already ran through the whole hash once;
do while (count &amp;gt;= 50); * find a manager with count &amp;lt; 50;
  rc = iter.next(); * next hash object;
  if rc ne 0 /* we have reached the "end" of the hash */
  then do;
    if runover then leave; * we've been here already, so exit the loop;
    runover = 1;
    rc = iter.first(); * reset the hash to its beginning;
  end;
end;
if runover and rc
then do; * obviously we've run out of managers with count &amp;lt;= 50;
  put 'No managers left';
  stop; * end the data step execution;
end;
count + 1; * increment;
test2.replace(); * and replace in the hash;
output; * output before stepping to the next hash object;
* if we don't do this now, the elements of the next hash iteration
  will end up in the output observation;
rc = iter.next(); * get next object;
if rc then rc = iter.first(); * if end, reset;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2016 09:51:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281023#M56910</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-29T09:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281026#M56911</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accounts;
datalines;
12
2
32
4
5
6
7
8
9
50
11
46
13
14
15
;
run;

data test2;
input managers$ count;
datalines;
a 48
b 45
c 49
;
run;

proc transpose data=test2 out=tr_test2;
id managers;
idlabel managers;
run;

proc sql;
	SELECT count(*)
	INTO :n_accounts
	FROM test;
quit;

data test3;
	set tr_test2;
	keep managers;
	array a_mngrs {*} a--c;

	if _N_=1 then index=1;

	do i=1 to &amp;amp;n_accounts.;
		if a_mngrs{index} lt 50 then do;
			a_mngrs{index}=a_mngrs{index}+1;
			managers=vname(a_mngrs{index});
			output;
		end;

		/* Probably a better way to do this */
		index=mod(index+1,dim(a_mngrs));
		if index=0 then index=dim(a_mngrs);
	end;
run;

data want;
	merge test test3 (in=a);
	if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2016 10:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281026#M56911</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T10:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281033#M56913</link>
      <description>&lt;P&gt;Explain the stopping condition a bit more ... how does count reach 50?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a solution involved sorting (and yet still got the answer you wanted) is that feasible or is your data set too large?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 11:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281033#M56913</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-29T11:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281118#M56939</link>
      <description>&lt;P&gt;Great work Gamotte. adding a little more to the problem. this sticks for only a b and c what if we have n no. of managers in dataset with different counts. Just to make it dynamic.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 14:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281118#M56939</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T14:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281119#M56940</link>
      <description>As you can see, the array is declared as&lt;BR /&gt;array a_mngrs {*} a--c;&lt;BR /&gt;That is all columns from a to c. If you have more managers, just indicate the first and the last instead of a and c.</description>
      <pubDate>Wed, 29 Jun 2016 14:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281119#M56940</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T14:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281120#M56941</link>
      <description>&lt;P&gt;That's fine but what if dont want to keep changing the first and last values. may be i just want my code to work for all datasets thrown at it.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 14:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281120#M56941</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T14:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281122#M56942</link>
      <description>You can use _NUMERIC_ instead of a--c to select all numeric columns of the transposed dataset.</description>
      <pubDate>Wed, 29 Jun 2016 14:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281122#M56942</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-29T14:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281126#M56944</link>
      <description>Awesome work man.!! Kudos. It works.</description>
      <pubDate>Wed, 29 Jun 2016 15:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Issue/m-p/281126#M56944</guid>
      <dc:creator>wizkid2050</dc:creator>
      <dc:date>2016-06-29T15:04:47Z</dc:date>
    </item>
  </channel>
</rss>

