<?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: Turn my variable (column) into observation (row) in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641164#M21813</link>
    <description>&lt;P&gt;Not sure if you wanted to keep a variable indicating order but, regardless, the %untranspose macro can easily do what you want. e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input studyid age bmi (adm_dt1-adm_dt3) (:yymmdd8.) (adm_dx1-adm_dx3) (:8.) 
		(adm_tx1-adm_tx3) (:8.);
	format adm_dt1-adm_dt3 yymmdd10.;
	datalines;
1 23 28.9 20180908 20181120 20190210 1 1 1 0 1 1
2 34 26.9 20170103 20171230 20180522 4 2 3 1 1 1
;
run;

filename ut url 'http://tiny.cc/untranspose_macro';
%include ut ;
%untranspose(data=have, out=want (drop=order), by=studyid age bmi,
  id=order, var=adm_dt adm_dx adm_tx)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Sun, 19 Apr 2020 20:50:11 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2020-04-19T20:50:11Z</dc:date>
    <item>
      <title>Turn my variable (column) into observation (row)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641142#M21805</link>
      <description>&lt;P&gt;Hello everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After some research I wasn't able to find a post with a similar problem so here it goes:&lt;/P&gt;&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="DataSet [Original].png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38436i321C5A8D1498F76E/image-size/large?v=v2&amp;amp;px=999" role="button" title="DataSet [Original].png" alt="DataSet [Original].png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It was collected in a longitudinal format with multiple Admissions ("Adm#1", "Adm#2", "Adm#3") following each other. I was hoping to look at each Admission individually, i.e. making Admission my observation/unit of analysis, regardless of the "Study ID". Ideally something that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="DataSet [Final].png" style="width: 210px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38437i29D4FF0AB8D1BA7E/image-dimensions/210x256?v=v2" width="210" height="256" role="button" title="DataSet [Final].png" alt="DataSet [Final].png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to recreate the dataset with TRANSPOSE but without success.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Miguel&lt;/P&gt;</description>
      <pubDate>Sun, 19 Apr 2020 18:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641142#M21805</guid>
      <dc:creator>MiguelCosta</dc:creator>
      <dc:date>2020-04-19T18:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: Turn my variable (column) into observation (row)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641145#M21807</link>
      <description>&lt;P&gt;Transpose is the correct method, but you likely want to use a data step for your problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example 3 in the first link or Example 2 in the second is what you want. If you can't get it to work, please show the code you've tried. I'd definitely recommend the first link, easier to follow IMO and less steps. However, the second method generalizes more easily if the number of variables can change over time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;Wide to Long:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/" target="_blank" rel="noopener"&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;/P&gt;
&lt;P&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;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: added the transpose option as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/323994"&gt;@MiguelCosta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After some research I wasn't able to find a post with a similar problem so here it goes:&lt;/P&gt;
&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="DataSet [Original].png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38436i321C5A8D1498F76E/image-size/large?v=v2&amp;amp;px=999" role="button" title="DataSet [Original].png" alt="DataSet [Original].png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It was collected in a longitudinal format with multiple Admissions ("Adm#1", "Adm#2", "Adm#3") following each other. I was hoping to look at each Admission individually, i.e. making Admission my observation/unit of analysis, regardless of the "Study ID". Ideally something that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="DataSet [Final].png" style="width: 210px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38437i29D4FF0AB8D1BA7E/image-dimensions/210x256?v=v2" width="210" height="256" role="button" title="DataSet [Final].png" alt="DataSet [Final].png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to recreate the dataset with TRANSPOSE but without success.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Miguel&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Apr 2020 18:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641145#M21807</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-19T18:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Turn my variable (column) into observation (row)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641156#M21809</link>
      <description>&lt;P&gt;Maybe something like this will work for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input studyid age bmi (adm_dt1-adm_dt3) (:yymmdd8.) (adm_dx1-adm_dx3) (:8.) 
		(adm_tx1-adm_tx3) (:8.);
	format adm_dt1-adm_dt3 yymmdd10.;
	datalines;
1 23 28.9 20180908 20181120 20190210 1 1 1 0 1 1
2 34 26.9 20170103 20171230 20180522 4 2 3 1 1 1
;
run;

data want;
	set have;
	array dt{3} adm_dt:;
	array dx{3} adm_dx:;
	array tx{3} adm_tx:;
	format admit_dt yymmdd10.;

	do i=1 to dim(dt);
		admit_dt=dt[i];
		admit_dx=dx[i];
		admit_tx=tx[i];
		output;
	end;
	drop adm_dt: adm_dx: adm_tx: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Apr 2020 19:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641156#M21809</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-04-19T19:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: Turn my variable (column) into observation (row)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641157#M21810</link>
      <description>&lt;P&gt;Or, just for fun, with SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input studyid age bmi (adm_dt1-adm_dt3) (:yymmdd8.) (adm_dx1-adm_dx3) (:8.) 
		(adm_tx1-adm_tx3) (:8.);
	format adm_dt1-adm_dt3 yymmdd10.;
	datalines;
1 23 28.9 20180908 20181120 20190210 1 1 1 0 1 1
2 34 26.9 20170103 20171230 20180522 4 2 3 1 1 1
;
run;

proc sql;
create table want as
select studyid, age, bmi, adm_dt1 format yymmdd10., adm_dx1, adm_tx1 from have
union ALL
select studyid, age, bmi, adm_dt2, adm_dx2, adm_tx2 from have
union ALL
select studyid, age, bmi, adm_dt3, adm_dx3, adm_tx3 from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 19 Apr 2020 19:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641157#M21810</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-19T19:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Turn my variable (column) into observation (row)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641164#M21813</link>
      <description>&lt;P&gt;Not sure if you wanted to keep a variable indicating order but, regardless, the %untranspose macro can easily do what you want. e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input studyid age bmi (adm_dt1-adm_dt3) (:yymmdd8.) (adm_dx1-adm_dx3) (:8.) 
		(adm_tx1-adm_tx3) (:8.);
	format adm_dt1-adm_dt3 yymmdd10.;
	datalines;
1 23 28.9 20180908 20181120 20190210 1 1 1 0 1 1
2 34 26.9 20170103 20171230 20180522 4 2 3 1 1 1
;
run;

filename ut url 'http://tiny.cc/untranspose_macro';
%include ut ;
%untranspose(data=have, out=want (drop=order), by=studyid age bmi,
  id=order, var=adm_dt adm_dx adm_tx)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sun, 19 Apr 2020 20:50:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Turn-my-variable-column-into-observation-row/m-p/641164#M21813</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2020-04-19T20:50:11Z</dc:date>
    </item>
  </channel>
</rss>

