<?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: Data Manipulation from long to wide - Proc Transpose or something else? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622601#M183161</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, thank you for replying. The reason why I want to convert to long is because I'm calculating a number of frequency tables and I find it easier if the data are long. Plus, I'll be doing some regression analyses and I have to have it long format.</description>
    <pubDate>Thu, 06 Feb 2020 03:19:08 GMT</pubDate>
    <dc:creator>sharonlee</dc:creator>
    <dc:date>2020-02-06T03:19:08Z</dc:date>
    <item>
      <title>Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622588#M183154</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Question&lt;/TD&gt;&lt;TD&gt;Response&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;beta&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;beta&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I want this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Q1&lt;/TD&gt;&lt;TD&gt;Q2&lt;/TD&gt;&lt;TD&gt;Q3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;beta&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use proc transpose as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc transpose data = have out = want;&lt;/P&gt;&lt;P&gt;by id;&lt;/P&gt;&lt;P&gt;var Question response;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My output is&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;col1&lt;/TD&gt;&lt;TD&gt;col2&lt;/TD&gt;&lt;TD&gt;col3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;alpha&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;beta&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;beta&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm investigating arrays (admittedly my knowledge is not so good with arrays), but I don't think this is possible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A workaround I think can be to create a dataset with just Q1 and then run a proc transpose.&amp;nbsp; However, I have over 100 questions, so even with a macro statement it is very onerous.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 00:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622588#M183154</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-02-06T00:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622593#M183156</link>
      <description>&lt;P&gt;how about this code using arrray statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data now;
  length id $10 Question 8 Response $1;
  infile cards;
  input id $ Question Response $;
cards;
ALPHA 1 Y
ALPHA 2 N
ALPHA 3 Y
BETA 1 N
BETA 2 N
;
run;

proc sort data=now out=tmp nodupkey;
  by descending Question;
run;
data _null_;
  set tmp(obs=1);
  call symputx('max_q',Question);/* set max of questions */
run;

proc sort data=now out=new;
  by id question;
run;

data new(keep=id q1-q&amp;amp;max_q);
  set new;
  by id question;
  array q{&amp;amp;max_q} $1;/* create array */
  retain q1-q&amp;amp;max_q;/* keep array variables to next observation */
  if first.id then do;/* init array value */
    do i=1 to &amp;amp;max_q;
      call missing(q{i});
    end;
  end;
  q{question}=Response;/* set response value to array variable by using question values as array subscript. */
  if last.id;/* output only last.id observations */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 01:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622593#M183156</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2020-02-06T01:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622594#M183157</link>
      <description>&lt;P&gt;May I ask why you want to transpose the data in first place? A long and narrow structure is better for most SAS procedures and often also easier to deal with in SAS data steps.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 01:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622594#M183157</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-06T01:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622596#M183158</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110042"&gt;@sharonlee&lt;/a&gt;&amp;nbsp; , Most SAS users and I will concur&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp; to keep your dataset long and narrow. The long and narrow offers enormous convenience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways proc transpose is rather straight forward&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID $	Question	Response $ ;
cards;
alpha	1	Y
alpha	2	N
alpha	3	Y
beta	1	N
beta	2	N
;

proc transpose data=have out=want(drop=_:) prefix=Q;
by id;
var response;
id Question;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 02:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622596#M183158</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-06T02:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622601#M183161</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, thank you for replying. The reason why I want to convert to long is because I'm calculating a number of frequency tables and I find it easier if the data are long. Plus, I'll be doing some regression analyses and I have to have it long format.</description>
      <pubDate>Thu, 06 Feb 2020 03:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622601#M183161</guid>
      <dc:creator>sharonlee</dc:creator>
      <dc:date>2020-02-06T03:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622602#M183162</link>
      <description>&lt;P&gt;Ok it's all good. Well, I posted the transpose in my previous post. I hope that helps.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 03:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622602#M183162</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-06T03:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Data Manipulation from long to wide - Proc Transpose or something else?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622619#M183166</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110042"&gt;@sharonlee&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;As proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, the ID statement enables you to identify a variable that will be used to name the new columns, which are col1, col2, ... by default. The option ‘prefix=Q’ will add the prefix ‘Q’ to these column names -&amp;gt; so Q1, Q2, ... as the modalities of question are 1, 2, ...)&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want (drop=_name_) prefix=Q;
	by id;
	var response;
	id question;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 08:00:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Manipulation-from-long-to-wide-Proc-Transpose-or-something/m-p/622619#M183166</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T08:00:30Z</dc:date>
    </item>
  </channel>
</rss>

