<?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: Transform a dataset from long to wide format and fill values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958627#M43055</link>
    <description>&lt;P&gt;Does below return what you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. Admission :date09. Discharge :date09. Age_class Age_end Index Value Total Age_class1 Age_class2 Age_class3 Age_class4 Age_class5; 
  format Admission date9. Discharge date9.;
cards;
0001 01JUL2014  16AUG2014    1    4   1   2.3  11.9   2.3   1.4   5   3.2    .
0001 13MAY2018  22JUN2018    3    4   0   1.4   .      .     .    .   .    .
0001 23JAN2019  25JAN2019    4    4   0   3.2   .      .     .    .   .    .
0002 13MAY2016  22SEP2016    1    5   1    2   7.9     2    0.3   0.2   5    0.4
0002 09JUL2023  10JUL2023    2    5   0   0.3   .      .     .    .   .    .
0002 12SEP2024  15SEP2024    3    5   0   0.2   .      .     .    .   .    .
0003 01JUL2014  18AUG2014    1    3   1    12  17.3    12   0.3   5   .    .
0003 07DEC2023  16DEC2023    2    3   0    0.3  .      .     .    .   .    . 
0004 12JAN2014  15JAN2014    1    2   1     2   2.1    2    0.1   .   .    .
0004 30MAY2019  13JUL2019    2    2   0    0.1   .     .     .    .   .    .
0005 30JUN2019  13OCT2019    5    5   1    4.1  4.1    .     .    .   .    4.1
0006 30JUN2019  13OCT2019    5    5   1    0  0    .     .    .   .    .
; 
run;

data want(drop=_:);
  if _n_=1 then 
    do;
      _val=0;
      dcl hash h1(dataset:'have(rename=(value=_val))', ordered:'y');
      h1.defineKey('id','age_class');
      h1.defineData('_val');
      h1.defineDone();
    end;
    set have;
    array age_class_derived{5} 8;

    if index=1 then
      do;
        /* distribute existing values */
        do _i=1 to dim(age_class_derived);
          if h1.find(key:id,key:_i)=0 then age_class_derived[_i]=_val;
        end;
        
        /* fill-up with max 5 until total reached */
        _total=round(sum(of age_class_derived[*]),.0000001);
        do _i=1 to dim(age_class_derived) while( _total&amp;lt;total );
          if missing(age_class_derived[_i]) then 
            do;
              age_class_derived[_i]=min(5,abs(sum(total,-_total)));
            end;
            _total=round(sum(of age_class_derived[*]),.0000001);
        end;
      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-1738925744070.png" style="width: 1155px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104447i2C6D80C71FABB9E6/image-dimensions/1155x231?v=v2" width="1155" height="231" role="button" title="Patrick_0-1738925744070.png" alt="Patrick_0-1738925744070.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I've made the assumption that the marked cells in your sample data were typos.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Feb 2025 10:56:53 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2025-02-07T10:56:53Z</dc:date>
    <item>
      <title>Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958618#M43054</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following table:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Age_class Age_end Index Value Total; 
  format Admission date9. Discharge date9.;
cards;
0001 01JUL2014  16AUG2014    1    4   1   2.3  11.9
0001 13MAY2018  22JUN2018    3    4   0   1.4   .
0001 23JAN2019  25JAN2019    4    4   0    3.2  .
0002 13MAY2016  22SEP2016    1    5   1    2   7.9
0002 09JUL2023  10JUL2023    2    5   0   0.3   .
0002 12SEP2024  15SEP2024    3    5   0   0.2   .
0003 01JUL2014  18AUG2014    1    3   1   12  17.3
0003 07DEC2023  16DEC2023    2    3   0   0.3   .
0004 12JAN2014  15JAN2014    1    2   1    2    2.1
0004 30MAY2019  13JUL2019    2    2   0   0.1   . 
0005 30JUN2019  13OCT2019    5    5   0   4.1   . 
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to get the following?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Age_class Age_end Index Value Total Age_class1 Age_class2 Age_class3 Age_class4 Age_class5; 
  format Admission date9. Discharge date9.;
cards;
0001 01JUL2014  16AUG2014    1    4   1   2.3  11.9   2.3   5   1.4   3.2    .
0001 13MAY2018  22JUN2018    3    4   0   1.4   .      .     .    .   .    .
0001 23JAN2019  25JAN2019    4    4   0   3.2   .      .     .    .   .    .
0002 13MAY2016  22SEP2016    1    5   1    2   7.9     2    0.3   0.2   5    0.4
0002 09JUL2023  10JUL2023    2    5   0   0.3   .      .     .    .   .    .
0002 12SEP2024  15SEP2024    3    5   0   0.2   .      .     .    .   .    .
0003 01JUL2014  18AUG2014    1    3   1    12  17.3    12   0.3   5   .    .
0003 07DEC2023  16DEC2023    2    3   0    0.3  .      .     .    .   .    . 
0004 12JAN2014  15JAN2014    1    2   1     2   2.1    2    0.1   .   .    .
0004 30MAY2019  13JUL2019    2    2   0    0.1   .     .     .    .   .    .
0005 30JUN2019  13OCT2019    5    5   1    4.1  4.1    .     .    .   .    4.1
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words, for each ID in DB there is the Age_class at each admission and an Age_class at exit (Age_end) from the study. Then, there is the "Value" variable that is the time the patient stay in the corresponding Age_class (before exit). There is also a "Total" time in the study. What I need is to transform the dataset DB into a wide format where there are columns corresponding to the age classes filled by the corresponding Value. Note that in this new dataset the row-wise total must be reached and must be equal to "Total" and so: if there is a jump in the age classes (Age_class variable) a value = 5 years must be added because the age-class interval is of max 5 years.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, in the new wide dataset (DB1) rows must be filled only where Index = 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 11:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958618#M43054</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-02-07T11:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958627#M43055</link>
      <description>&lt;P&gt;Does below return what you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. Admission :date09. Discharge :date09. Age_class Age_end Index Value Total Age_class1 Age_class2 Age_class3 Age_class4 Age_class5; 
  format Admission date9. Discharge date9.;
cards;
0001 01JUL2014  16AUG2014    1    4   1   2.3  11.9   2.3   1.4   5   3.2    .
0001 13MAY2018  22JUN2018    3    4   0   1.4   .      .     .    .   .    .
0001 23JAN2019  25JAN2019    4    4   0   3.2   .      .     .    .   .    .
0002 13MAY2016  22SEP2016    1    5   1    2   7.9     2    0.3   0.2   5    0.4
0002 09JUL2023  10JUL2023    2    5   0   0.3   .      .     .    .   .    .
0002 12SEP2024  15SEP2024    3    5   0   0.2   .      .     .    .   .    .
0003 01JUL2014  18AUG2014    1    3   1    12  17.3    12   0.3   5   .    .
0003 07DEC2023  16DEC2023    2    3   0    0.3  .      .     .    .   .    . 
0004 12JAN2014  15JAN2014    1    2   1     2   2.1    2    0.1   .   .    .
0004 30MAY2019  13JUL2019    2    2   0    0.1   .     .     .    .   .    .
0005 30JUN2019  13OCT2019    5    5   1    4.1  4.1    .     .    .   .    4.1
0006 30JUN2019  13OCT2019    5    5   1    0  0    .     .    .   .    .
; 
run;

data want(drop=_:);
  if _n_=1 then 
    do;
      _val=0;
      dcl hash h1(dataset:'have(rename=(value=_val))', ordered:'y');
      h1.defineKey('id','age_class');
      h1.defineData('_val');
      h1.defineDone();
    end;
    set have;
    array age_class_derived{5} 8;

    if index=1 then
      do;
        /* distribute existing values */
        do _i=1 to dim(age_class_derived);
          if h1.find(key:id,key:_i)=0 then age_class_derived[_i]=_val;
        end;
        
        /* fill-up with max 5 until total reached */
        _total=round(sum(of age_class_derived[*]),.0000001);
        do _i=1 to dim(age_class_derived) while( _total&amp;lt;total );
          if missing(age_class_derived[_i]) then 
            do;
              age_class_derived[_i]=min(5,abs(sum(total,-_total)));
            end;
            _total=round(sum(of age_class_derived[*]),.0000001);
        end;
      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-1738925744070.png" style="width: 1155px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104447i2C6D80C71FABB9E6/image-dimensions/1155x231?v=v2" width="1155" height="231" role="button" title="Patrick_0-1738925744070.png" alt="Patrick_0-1738925744070.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I've made the assumption that the marked cells in your sample data were typos.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 10:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958627#M43055</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-02-07T10:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958630#M43056</link>
      <description>&lt;P&gt;Really really sorry: for ID 0001 age class 3 = 1.4 while age_class = 2 is 5. I will edit my question soon.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 11:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958630#M43056</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-02-07T11:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958646#M43057</link>
      <description>&lt;P&gt;The desired result does not make much sense.&lt;/P&gt;
&lt;P&gt;Either make a wide dataset with one observation per ID.&lt;/P&gt;
&lt;P&gt;So perhaps something like this?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1738940887166.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104454i4C489C418A5DDDE5/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1738940887166.png" alt="Tom_0-1738940887166.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or leave it in the tall format.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 15:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958646#M43057</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-07T15:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958648#M43058</link>
      <description>&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, I don't see the point of doing this transformation from long to wide. What is the benefit here of creating a wide data set as you stated above? What can you do with such a wide data set that you can't do with the long data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As things stand so far in this thread, this is the &lt;A href="https://en.wikipedia.org/wiki/XY_problem#:~:text=The%20XY%20problem%20is%20a,(Y%20or%20Why%3F)." target="_self"&gt;XY problem&lt;/A&gt;, where it seems like you are tightly focused on one specific programming task, and possibly you have chosen to go down a path this is inefficient and hard to program; and if we knew where you wanted to go, we could help figure out a better path that is easier to program.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 15:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958648#M43058</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-07T15:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958697#M43059</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, I don't see the point of doing this transformation from long to wide. What is the benefit here of creating a wide data set as you stated above? What can you do with such a wide data set that you can't do with the long data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As things stand so far in this thread, this is the &lt;A href="https://en.wikipedia.org/wiki/XY_problem#:~:text=The%20XY%20problem%20is%20a,(Y%20or%20Why%3F)." target="_self"&gt;XY problem&lt;/A&gt;, where it seems like you are tightly focused on one specific programming task, and possibly you have chosen to go down a path this is inefficient and hard to program; and if we knew where you wanted to go, we could help figure out a better path that is easier to program.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;From several of the other posts by the OP I would say this set &lt;STRONG&gt;might&lt;/STRONG&gt; be related to interpolating between the "wide" values prior to transposing back to a long form.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would say that OP posts have all the symptoms of an XY problem with never describing actual starting data and where to go but keeps asking how to do one task and often poorly described at that.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 19:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958697#M43059</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-02-07T19:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Transform a dataset from long to wide format and fill values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958749#M43065</link>
      <description>&lt;P&gt;Just for having some fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Age_class Age_end Index Value Total; 
  format Admission date9. Discharge date9.;
cards;
0001 01JUL2014  16AUG2014    1    4   1   2.3  11.9
0001 13MAY2018  22JUN2018    3    4   0   1.4   .
0001 23JAN2019  25JAN2019    4    4   0    3.2  .
0002 13MAY2016  22SEP2016    1    5   1    2   7.9
0002 09JUL2023  10JUL2023    2    5   0   0.3   .
0002 12SEP2024  15SEP2024    3    5   0   0.2   .
0003 01JUL2014  18AUG2014    1    3   1   12  17.3
0003 07DEC2023  16DEC2023    2    3   0   0.3   .
0004 12JAN2014  15JAN2014    1    2   1    2    2.1
0004 30MAY2019  13JUL2019    2    2   0   0.1   . 
0005 30JUN2019  13OCT2019    5    5   0   4.1   . 
; 
run;
proc sql noprint;
create table summary as
select ID,sum(Total)-sum(Value) as balance
 from DB
  group by ID;
select max(Age_class) into :n trimmed from DB;
quit;
data token;
set summary;
temp=balance;n=0;
if balance&amp;lt;5 then do;n=1;output;end;
 else do;
   do until(balance&amp;lt;5);
     n+1;balance=5;output;
     balance=temp-5;
     temp=temp-5;
   end;
   n+1;output;
 end;
drop temp;
run;
proc transpose data=DB out=transpose(drop=_name_) prefix=Age_class;
by id ;
id Age_class;
var value;
run;
data want;
if _n_=1 then do;
if 0 then set DB;
retain Age_class1-Age_class&amp;amp;n.; *Change the order of variables;
if 0 then set token;
declare hash h(dataset:'token');
h.definekey('id','n');
h.definedata('balance');
h.definedone();
end;
merge DB transpose;
by id;
array x{*} Age_class1-Age_class&amp;amp;n.;
n=0;
if first.id then do;
do i=1 to dim(x);
  if missing(x{i}) then do;
    n+1;call missing(balance);rc=h.find();x{i}=balance;rc=h.remove();
  end;
end;
end;
drop rc n i balance;
output;
call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Feb 2025 07:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transform-a-dataset-from-long-to-wide-format-and-fill-values/m-p/958749#M43065</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-02-08T07:54:54Z</dc:date>
    </item>
  </channel>
</rss>

