<?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 can i transpose 2 variable in one go? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959429#M374327</link>
    <description>&lt;P&gt;Check the MERGE skill proposed by Me and Arthur.T :&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input office $ dept $ year seq $ code $ me_amt me_pct;
    datalines;
LA IT 2024 1 101 10000 0
LA IT 2024 1 102 66 12
MI FIN 2024 1 101 333 1
MI FIN 2024 1 102 98.7 12.3
;
run;
proc sql noprint;
select distinct catt('have(where=(code="',code,'" ) 
 rename=(me_amt=me_amt_',code,' me_pct=me_pct_',code,'))') into :merge separated by ' '
 from have;
quit;
data want;
merge &amp;amp;merge.;
by office dept year;
drop code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1739844777664.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104699iCC99C9136F6FFBBA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1739844777664.png" alt="Ksharp_0-1739844777664.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, 18 Feb 2025 02:13:14 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2025-02-18T02:13:14Z</dc:date>
    <item>
      <title>How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959344#M374296</link>
      <description>&lt;P&gt;How can i transpose 2 variable in one go?&lt;/P&gt;&lt;P&gt;I have a table that looks like this:&lt;/P&gt;&lt;P&gt;data testdata;&lt;/P&gt;&lt;P&gt;office='LA';&lt;/P&gt;&lt;P&gt;dept='IT';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;code='101';&lt;/P&gt;&lt;P&gt;me_amt=10000;&lt;/P&gt;&lt;P&gt;me_pct=0;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;office='LA';&lt;/P&gt;&lt;P&gt;dept='IT';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;code='102';&lt;/P&gt;&lt;P&gt;me_amt=66;&lt;/P&gt;&lt;P&gt;me_pct=12;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;office='MI';&lt;/P&gt;&lt;P&gt;dept='FIN';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;code='101';&lt;/P&gt;&lt;P&gt;me_amt=333;&lt;/P&gt;&lt;P&gt;me_pct=1;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;office='MI';&lt;/P&gt;&lt;P&gt;dept='FIN';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;code='102';&lt;/P&gt;&lt;P&gt;me_amt=98.7;&lt;/P&gt;&lt;P&gt;me_pct=12.3;&lt;/P&gt;&lt;P&gt;output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want to achieve is something like this:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;office='LA';&lt;/P&gt;&lt;P&gt;dept='IT';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;amtAAA=10000;&lt;/P&gt;&lt;P&gt;pctAAA=0;&lt;/P&gt;&lt;P&gt;amtBBB=66;&lt;/P&gt;&lt;P&gt;pctBBB=12;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;office='MI';&lt;/P&gt;&lt;P&gt;dept='FIN';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;seq='1';&lt;/P&gt;&lt;P&gt;amtAAA=333;&lt;/P&gt;&lt;P&gt;pctAAA=1;&lt;/P&gt;&lt;P&gt;amtBBB=98.7;&lt;/P&gt;&lt;P&gt;pctBBB=12.3;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to make it a single row for each unique office + dept + year + seq.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can think of the way of achieving this for only "me_amt" is by first hardcoding a new column (flag) with the name "amtAAA".&lt;BR /&gt;Then, sort it with proc sort by&amp;nbsp;office + dept + year + seq.&lt;/P&gt;&lt;P&gt;Then, transpose it with a proc transpose statement with :&lt;BR /&gt;id flag;&lt;BR /&gt;var me_amt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, how do i achieve this by including "me_pct" as well? as my transpose statement will only work for transposing "me_amt".&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 10:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959344#M374296</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-02-17T10:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959355#M374297</link>
      <description>&lt;P&gt;I didn't really understand where you're taking naming components AAA and BBB from so I've used variable code instead. Does below return what you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input office $ dept $ year seq $ code $ me_amt me_pct;
    datalines;
LA IT 2024 1 101 10000 0
LA IT 2024 1 102 66 12
MI FIN 2024 1 101 333 1
MI FIN 2024 1 102 98.7 12.3
;
run;

proc sql noprint;
  select 
    distinct 
    cats('me_amt_',code), cats('me_pct_',code)
      into :me_amt separated by ' ', :me_pct separated by ' '
  from have
  ;
quit;

proc sort data=have out=want;
  by office dept year seq;
run;

data want(drop=_: me_amt me_pct);
  set want;
  by office dept year seq;
  array a_me_amt{*} &amp;amp;me_amt;
  array a_me_pct{*} &amp;amp;me_pct;
  retain a_me_amt a_me_pct;
  do _i=1 to dim(a_me_amt);
    if scan(vname(a_me_amt[_i]),-1,'_')=code then
      do;
        a_me_amt[_i]=me_amt;
        a_me_pct[_i]=me_pct;       
      end;
  end;
  if last.office then 
    do;
      output;
      call missing(of _all_);
    end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1739793072059.png" style="width: 685px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104678i6B9E35B98F56A8B0/image-dimensions/685x88?v=v2" width="685" height="88" role="button" title="Patrick_0-1739793072059.png" alt="Patrick_0-1739793072059.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>Mon, 17 Feb 2025 11:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959355#M374297</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-02-17T11:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959358#M374298</link>
      <description>&lt;P&gt;I think you'd just want the above to be: &lt;BR /&gt;if last.seq then...&lt;BR /&gt;...instead of:&lt;BR /&gt;if last.office then...&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 13:35:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959358#M374298</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-02-17T13:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959363#M374301</link>
      <description>&lt;P&gt;When I read the original question, and then see the difficult programming that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; provides, I feel compelled to ask&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;&amp;nbsp; why this data set needs to be transposed in the first place. What is the benefit of re-arranging the data in this fashion? It seems to me that re-arranging the data set just makes programming the next step(s) much more difficult. If the goal is to create a wide table for output, then no transpose is needed, and PROC REPORT will do this without the difficulties of transposing.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 14:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959363#M374301</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-17T14:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959371#M374306</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;! While the below solution is two steps, it does prevent you needing to sort and merge and stays entirely within PROC TRANSPOE. For this example, you can take advantage of the&amp;nbsp;&lt;EM&gt;copy&lt;/EM&gt; statement in PROC TRANSPOSE to bring the variables you need forward. We'll use the variable&amp;nbsp;&lt;EM&gt;code&lt;/EM&gt; to keep track of each variable's row position and add a prefix for its name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=testdata 
               out=testdata2 
               prefix=me_amt_;
    by office dept year seq;
    id code;
    copy me_pct code;
    var me_amt;
run;

proc transpose data=testdata2 
               out=want(drop=_NAME_) 
               prefix=me_pct_;
    by office dept year seq;
    id code;
    copy me_amt:;
    var me_pct;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's break this down. We'll start by looking at the first step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;office	dept	year	seq	me_pct	code	_NAME_	me_amt_101	me_amt_102
LA	IT	2024	1	0	101	me_amt	10000	        66
LA	IT	2024	1	12	102		.	        .
MI	FI	2024	1	1	101	me_amt	333	        98.7
MI	FI	2024	1	12.3	102		.	        .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice we have&amp;nbsp;&lt;EM&gt;me_amt&lt;/EM&gt; in the format we want. Now we just need to get&amp;nbsp;&lt;EM&gt;me_pct&lt;/EM&gt;. We copied forward the values of&amp;nbsp;&lt;EM&gt;me_pct&lt;/EM&gt; and&amp;nbsp;&lt;EM&gt;code&lt;/EM&gt; which we are using as our ID. For the final step we just need to do the opposite: we will transpose by&amp;nbsp;&lt;EM&gt;office dept year seq&lt;/EM&gt;, get our ID from&amp;nbsp;&lt;EM&gt;code&lt;/EM&gt;, then copy forward&amp;nbsp;all the&amp;nbsp;&lt;EM&gt;me_amt&lt;/EM&gt; variables.&amp;nbsp; This results in the final dataset that we want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;office	dept	year	seq	me_amt_101	me_amt_102	me_pct_101	me_pct_102
LA	IT	2024	1	10000	        66	        0	        12
MI	FI	2024	1	333	        98.7	        1	        12.3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 15:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959371#M374306</guid>
      <dc:creator>Stu_SAS</dc:creator>
      <dc:date>2025-02-17T15:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959372#M374307</link>
      <description>&lt;P&gt;Just for fun. Two steps with some macros, for lazy-typers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tr(var);
proc transpose data=testdata out=&amp;amp;var.(drop=_name_) prefix=&amp;amp;var._;
by office dept year seq;
id code;
var &amp;amp;var.;
run;
%mend tr;


%tr(me_amt);
%tr(me_pct);

data want_12;
  merge me_:;
  by office dept year seq;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 15:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959372#M374307</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-02-17T15:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959375#M374309</link>
      <description>&lt;P&gt;You can do this with two proc transpose steps.&amp;nbsp; The first one converts your original "middle" format dataset into an actual "tall" format dataset.&amp;nbsp; Then the second one converts to a "wide" format dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your wallpaper code for making the dataset into a simple data step that reads from in-line data.&amp;nbsp; Notice how the code is not only easier to write but the data it represents is much clearer.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input office $ dept $ year seq $ code $ me_amt me_pct;
datalines;
LA IT  2024 1 101 10000  0
LA IT  2024 1 102    66 12
MI FIN 2024 1 101   333  1
MI FIN 2024 1 102  98.7 12.3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's run the two transpose steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=tall;
  by office dept year seq code;
  var me_amt me_pct;
run;

proc transpose data=tall out=want(drop=_name_) delim=_ ;
  by office dept year seq ;
  id _name_ code ;
  var col1 ;
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="Screenshot 2025-02-17 at 10.53.41 AM.png" style="width: 774px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104682i90BAF09176D64168/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-02-17 at 10.53.41 AM.png" alt="Screenshot 2025-02-17 at 10.53.41 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;This method will work for almost any number of variables, as long as they are all of the same type.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 15:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959375#M374309</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-17T15:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can i transpose 2 variable in one go?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959429#M374327</link>
      <description>&lt;P&gt;Check the MERGE skill proposed by Me and Arthur.T :&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input office $ dept $ year seq $ code $ me_amt me_pct;
    datalines;
LA IT 2024 1 101 10000 0
LA IT 2024 1 102 66 12
MI FIN 2024 1 101 333 1
MI FIN 2024 1 102 98.7 12.3
;
run;
proc sql noprint;
select distinct catt('have(where=(code="',code,'" ) 
 rename=(me_amt=me_amt_',code,' me_pct=me_pct_',code,'))') into :merge separated by ' '
 from have;
quit;
data want;
merge &amp;amp;merge.;
by office dept year;
drop code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1739844777664.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104699iCC99C9136F6FFBBA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1739844777664.png" alt="Ksharp_0-1739844777664.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, 18 Feb 2025 02:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-transpose-2-variable-in-one-go/m-p/959429#M374327</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-02-18T02:13:14Z</dc:date>
    </item>
  </channel>
</rss>

