<?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: reshaping data from long to wide in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913245#M359957</link>
    <description>&lt;P&gt;Don't share data as photographs.&amp;nbsp; Nobody wants to re-type your data.&lt;/P&gt;</description>
    <pubDate>Sun, 28 Jan 2024 15:27:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-28T15:27:31Z</dc:date>
    <item>
      <title>reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913241#M359953</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-01-28 075852.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93009iA966A26E94D5FC7F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-01-28 075852.png" alt="Screenshot 2024-01-28 075852.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Hi, I would like to know how can I reshape the long data to the expected wide data. I tried the code below, however, it can only move one column at a time and it cannot shape the table to what I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;data want2; set want1; retain ID; by ID; if first.ID then your_id=1; else your_id=your_id+1; run;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 13:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913241#M359953</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2024-01-28T13:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913242#M359954</link>
      <description>&lt;P&gt;You do not want a dataset in this layout, as it is a pain in the ass to work with.&lt;/P&gt;
&lt;P&gt;If you need this for reporting purposes, PROC REPORT is the tool:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=want1;
column id type,(dose length);
define id / group;
define type / across;
define dose / analysis;
define length / analysis;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2024 13:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913242#M359954</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-01-28T13:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913243#M359955</link>
      <description>&lt;P&gt;Not for the report, I need the wide data for the further Cox proportional analysis. Each type is an individual variable, that's why I have to convert them. I don't think the proc phreg can apply when there are multiple observations under the same person.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 14:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913243#M359955</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2024-01-28T14:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913244#M359956</link>
      <description>&lt;P&gt;That does not make sense with the data you showed.&amp;nbsp; Why would create variables named DOSE2 that might sometimes have the value of the dose of TYPE=4 and sometimes the value of dose for TYPE= something else?&amp;nbsp; Why not call the variable DOSE4 for the dose of type 4?&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 15:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913244#M359956</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-28T15:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913245#M359957</link>
      <description>&lt;P&gt;Don't share data as photographs.&amp;nbsp; Nobody wants to re-type your data.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 15:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913245#M359957</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-28T15:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913248#M359958</link>
      <description>&lt;P&gt;If you are just talking about reshaping then PROC SUMMARY can do that using IDGROUP.&amp;nbsp; You will need to know the maximum count in advance (easy enough to calculate and put into a macro variable).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=original;
  by id ;
  output out=want idgroup(out[5]&amp;nbsp;(type&amp;nbsp;dose&amp;nbsp;length)=)&amp;nbsp;;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the data is not already sorted by ID then use CLASS instead of BY and add NWAY option to the PROC SUMMARY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you didn't provide any example data let's use SASHELP.CLASS to see how it works.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class(obs=10) nway;
  class sex;
  output out=want idgroup(out[5] (name age)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1706456628597.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93010i1EA2D48E4F6EF533/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1706456628597.png" alt="Tom_0-1706456628597.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I do not see how that layout is going to help you.&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;</description>
      <pubDate>Sun, 28 Jan 2024 15:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913248#M359958</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-28T15:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913272#M359961</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/427928"&gt;@LarissaW&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Not for the report, I need the wide data for the further Cox proportional analysis. Each type is an individual variable, that's why I have to convert them. I don't think the proc phreg can apply when there are multiple observations under the same person.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I am not an expert on Cox proportional analysis, but it seems to me that you have a problem here that has nothing to do with SAS and has nothing to do with converting the data set to a new arrangement. It is my understanding (and I admit I could be 100% wrong on this) that to fit a Cox proportional hazard model, you can't have multiple time values and multiple doses for each patient/subject, and so I think you need to straighten out what to do when fitting a Cox proportional hazard model when a patient/subject has multiple time values and multiple doses. I don't think stringing them all out in one record in a SAS data set is the right approach. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So a better question to ask, which should be entitled "How to Fit a COX proportional hazard model to this data", should ignore the re-arranging the data issue entirely. Just show your original (long) data set and ask how to fit the model to it. (see &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 42&lt;/A&gt;)&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 16:40:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913272#M359961</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-28T16:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913274#M359963</link>
      <description>&lt;P&gt;Here the type is the smoking type and the dose and years are the corresponding number of cigarettes and smoking length. People change the type of smoking, dose and length over time, so there are several observations under the same person. Because some of the types overlap, so each type of smoking is an individual variable that interacts with each other.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 17:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913274#M359963</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2024-01-28T17:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913277#M359966</link>
      <description>&lt;P&gt;I'm pretty sure the Cox model can add a time-varying variable. However, I didn't plan to combine the type, dose and year, because they described the same sample from different aspects. I'd like to try the correlation of each of them with the outcomes. I know how to do the Cox proportional part but for my purpose on the Cox model, I need to transpose the long data to the wide data.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 17:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913277#M359966</guid>
      <dc:creator>LarissaW</dc:creator>
      <dc:date>2024-01-28T17:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913279#M359968</link>
      <description>&lt;P&gt;I didn't say Cox proportional hazard model cannot have time varying variable. I said I don't think it can have MULTIPLE time periods and multiple dosages for each subject. Maybe I'm wrong, but that's my limited understanding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please start a new thread. Ask the question the way I suggested, with the title that I suggested. In your two threads now where you focus on re-arranging the data, you have not gotten close to anything that can be used to fit a Cox proportional model, and have been advised by several people not to re-arrange the data as you want it to be re-arranged.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2024 19:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913279#M359968</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-28T19:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913284#M359972</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/427928"&gt;@LarissaW&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Here the type is the smoking type and the dose and years are the corresponding number of cigarettes and smoking length. People change the type of smoking, dose and length over time, so there are several observations under the same person. Because some of the types overlap, so each type of smoking is an individual variable that interacts with each other.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you don't want to get a generic transpose.&amp;nbsp; Instead you want to create new variables with meaning like NUMBER OF CIGARETTES, NUMBER OF CIGARS, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you type codes are simple 1,2,3 then just use them as an index into an array (or multiple arrays).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.id);
    set have;
    by id;
    array count num_cigarettes num_pipes num_cigars;
    array years year_cigarettes year_pipes year_cigars;
    count[type] = count;
    years[type] = length;
  end;
  drop type count length;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2024 21:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913284#M359972</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-28T21:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: reshaping data from long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913303#M359982</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/proc-transpose-dataset/m-p/912923#M83262" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/proc-transpose-dataset/m-p/912923#M83262&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 id type $ dose $ length :mmddyy10.;
   format length mmddyy10.;
datalines;
1  A   OH 05-23-2023
1  A   MI 11-30-2023
2  C   TX 01-22-2023
2  C   CA 04-14-2023
2  C   MO 07-25-2023
3  F   OH 06-22-2023
3  F   IN 12-12-2023
3  K   WA 08-01-2023
;

data temp;
 set have;
 by id ;
 if first.id then n=0;
 n+1;
run;
proc sql noprint;
select distinct catt('temp(where=(n=',n,') rename=(type=type',n,' dose=dose',n,' length=length',n,'))') 
       into :merge separated by ' '
  from temp;
quit;
data want;
 merge &amp;amp;merge.;
 by id;
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2024 02:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reshaping-data-from-long-to-wide/m-p/913303#M359982</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-01-29T02:17:36Z</dc:date>
    </item>
  </channel>
</rss>

