<?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: Help with a complicated reshape in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707603#M217275</link>
    <description>Great links, thanks so much</description>
    <pubDate>Tue, 22 Dec 2020 03:09:16 GMT</pubDate>
    <dc:creator>JenniferBernard</dc:creator>
    <dc:date>2020-12-22T03:09:16Z</dc:date>
    <item>
      <title>Help with a complicated reshape</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707571#M217264</link>
      <description>&lt;P&gt;I'm trying to do a reshape to long a dataset that preserves info about firm ids and establishment ids. A person (id) can have multiple jobs at a time. I can transpose everything to long, but what I want looks more like this:&amp;nbsp;&lt;BR /&gt;id_1 firmid1 estabid1 q1_value1 firmid2 estabid2 q1_value2 firmid3 estabid3 q1_value3&lt;BR /&gt;id_1 firmid1 estabid1 q2_value1 firmid2 estabid2 q2_value2 firmid3 estabid3 q2_value3&lt;BR /&gt;id_1 firmid1 estabid1 q3_value1 firmid2 estabid2 q3_value2 firmid3 estabid3 q3_value3&lt;BR /&gt;id_1 firmid1 estabid1 q4_value1 firmid2 estabid2 q4_value2 firmid3 estabid3 q4_value3&lt;BR /&gt;id_2 firmid1 estabid1 q1_value1 . . . . . .&lt;BR /&gt;id_2 firmid1 estabid1 q2_value1 . . . . . .&lt;BR /&gt;id_2 firmid1 estabid1 q3_value1 . . . . . .&lt;BR /&gt;id_2 firmid1 estabid1 q4_value1 . . . . . .&lt;BR /&gt;(In the example above, id_1 has three jobs, id_2 had one, etc.)&lt;BR /&gt;&lt;BR /&gt;This dataset is HUGE... think q1-q124 and millions of ids, so trying to do it as efficiently as possible.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to loop using first/last statements to make the code below work?&amp;nbsp; If there is a better way to do this, I'm open to any ideas.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $10. firmid $5. estabid $4. q1 q2 q3 q4;
  cards;
123456789 1001 000 100 125 125 150
123456789 1002 000	25 25 . .
123456789 1003 000	5 . . .
223456789 1002 000	25 25 25 25
323456789 1002 001 50 50 . . 
423456789 1004 000 100 125 125 100
423456789 1005 001 5 . . .
523456789 1006 001 100 . 100 .
523456789 1006 002 . 100 . 100
;
run;

proc transpose data=have out=want2 prefix=earn name=qtr;
   by id firmid estabid;
   run;

data want2(drop=q q2);
	set want2(rename=(qtr=q));
	q2 = compress(q, ' ', 'A');
	qtr=input(q2,8.);
	run;
	
proc sort data=want2;
by id;
run;
   
data want3 (keep=id qtr firmid1 estabid1 firmid2 estabid2 firmid3 estabid3 earn_job1 earn_job2 earn_job3);
	set want2;
	by id;
	length firmid1-firmid3 $5. estabid1-estabid3 $4. earn_job1-earn_job3 8;
	retain firmid1 estabid1 firmid2 estabid2 firmid3 estabid3 earn_job1 earn_job2 earn_job3;
	
	array afirmid{1:3} firmid1-firmid3;
	array aestabid{1:3} estabid1-estabid3;
	array aearn{1:3} earn_job1-earn_job3;
	
	if first.id then do;
		do i=1 to 3;
		afirmid{i}="    ";
		aestabid{i}="   ";
		aearn{i}=.;
		end;
	end;
	
	/*I am stuck here! Something like:&lt;BR /&gt;       if qtr ge 1 then do;&lt;BR /&gt;       afirmid{qtr}=firmid;&lt;BR /&gt;       aestabid{qtr}=estabid;&lt;BR /&gt;       aearn{qtr}=earn1;&lt;BR /&gt;       end;&lt;BR /&gt;       output;&lt;BR /&gt;    This obviously doesn't work */

	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks for taking a look!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 21:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707571#M217264</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2020-12-21T21:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a complicated reshape</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707582#M217266</link>
      <description>&lt;P&gt;The tutorials below cover all that you need, in this case, you may want to explore the last option - the double transpose for an extra wide data set or you may also want to consider the custom macro, A Better Way to Flip documented here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-a-dataset/ta-p/433620" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-a-dataset/ta-p/433620&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;Long to Wide:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Wide to Long:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;And sometimes a double transpose is needed for extra wide data sets:&lt;/STRONG&gt;&lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd" target="_blank"&gt;https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 22:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707582#M217266</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-12-21T22:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a complicated reshape</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707603#M217275</link>
      <description>Great links, thanks so much</description>
      <pubDate>Tue, 22 Dec 2020 03:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-complicated-reshape/m-p/707603#M217275</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2020-12-22T03:09:16Z</dc:date>
    </item>
  </channel>
</rss>

