<?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 How to concatenate multiple rows into one row with additional variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646312#M193308</link>
    <description>&lt;P&gt;If I have a dataset with X rows and Y columns, I'm trying to essentially make every Z number of sequential rows concatenated into one, so I end up with X/Z rows and Y*Z columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, X=6, Y=3, Z=2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;VAR1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;VAR2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;VAR3&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&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;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/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&gt;17&lt;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems simple enough, but I can't quite figure out a way that works. I should also note that the for the data I am using this on, X will always be divisible by Z. Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data provided above are just an example to show the type of thing I'm looking for. In reality, the dataset will have some multiple of 24 as the number of rows. Each row represents one hour out of one day, with each 24 rows coming from the same day. There will be 120 columns of numeric data for each row (no missing data). The goal is to get to the point of having each row be one day, with 2,880 (120x24) columns of data. What I'm going to end up doing is writing a macro to iterate the steps over multiple datasets with a different number of "days" (hence different number of total rows). Thanks to everyone who has responded!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 May 2020 22:54:46 GMT</pubDate>
    <dc:creator>tmcwill</dc:creator>
    <dc:date>2020-05-08T22:54:46Z</dc:date>
    <item>
      <title>How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646312#M193308</link>
      <description>&lt;P&gt;If I have a dataset with X rows and Y columns, I'm trying to essentially make every Z number of sequential rows concatenated into one, so I end up with X/Z rows and Y*Z columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, X=6, Y=3, Z=2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;VAR1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;VAR2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG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/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;17&lt;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems simple enough, but I can't quite figure out a way that works. I should also note that the for the data I am using this on, X will always be divisible by Z. Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data provided above are just an example to show the type of thing I'm looking for. In reality, the dataset will have some multiple of 24 as the number of rows. Each row represents one hour out of one day, with each 24 rows coming from the same day. There will be 120 columns of numeric data for each row (no missing data). The goal is to get to the point of having each row be one day, with 2,880 (120x24) columns of data. What I'm going to end up doing is writing a macro to iterate the steps over multiple datasets with a different number of "days" (hence different number of total rows). Thanks to everyone who has responded!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 22:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646312#M193308</guid>
      <dc:creator>tmcwill</dc:creator>
      <dc:date>2020-05-08T22:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646313#M193309</link>
      <description>&lt;P&gt;Your problem is perhaps less complicated and could be solved by a procedure like TRANSPOSE. It is not clear from your description. However, if you really want a solution as described, here is one solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have ;
   input var1 var2 var3 ;
   cards ;
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
;
run ;

data want (drop=_:) ;
   set have ;
   retain _v1 - _v3 ;
   array _v(3) ;
   array var(6) ;
   if mod(_n_,2)=0 then 
      do ;
         do _i=4 to dim(var) ;
            var(_i) = var(_i-3) ;
            var(_i-3) = _v(_i-3) ;
         end ;
         output ;
      end ;
   do _i = 1 to dim(_v) ;
      _v(_i) = var(_i) ;
   end ;
run ; 
      &lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 21:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646313#M193309</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-08T21:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646316#M193312</link>
      <description>&lt;P&gt;Is the example data your actual data or some sort of&amp;nbsp; example? If an example then you may need to tell us if any of the variables are character and what the lengths will need to be.&lt;/P&gt;
&lt;P&gt;Will the data &lt;STRONG&gt;always &lt;/STRONG&gt;have a structure such that the number of cells is exactly equal to the x times y?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way is to write out the data and read it:&lt;/P&gt;
&lt;PRE&gt;data have;
  input v1 v2 v3;
datalines;
1	2	3
4	5	6
7	8	9
10	11	12
13	14	15
16	17	18
;
proc export data=have
    outfile='c:\file.csv'
    replace
    dbms=dlm;
    delimiter=',';
run;

data want;
   infile  'c:\file.csv' dlm=',' flowover firstobs=2;
   input v1 - v6; /*&amp;lt;= the magic 6 is your y*z value*/
run;
&lt;/PRE&gt;
&lt;P&gt;But since you don't provide rules for if you have 30 cells and want 8 variables ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 21:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646316#M193312</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-08T21:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646319#M193314</link>
      <description>&lt;P&gt;How about a couple of DO loops?&amp;nbsp; Put the outer one around the SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input VAR1	VAR2	VAR3 ;
cards;
1	2	3
4	5	6
7	8	9
10	11	12
13	14	15
16	17	18
;

%let rows=2;
%let cols=3;
%let new_cols=%eval(&amp;amp;rows*&amp;amp;cols);

data want;
do row=1 to &amp;amp;rows;
  set have;
  array var [&amp;amp;cols] ;
  array newvar [&amp;amp;new_cols] ;
  do col=1 to &amp;amp;cols;
    index=sum(index,1);
    newvar[index] = var[col];
  end;
end;
drop var: row col index;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    newvar1    newvar2    newvar3    newvar4    newvar5    newvar6

 1         1          2          3          4          5          6
 2         7          8          9         10         11         12
 3        13         14         15         16         17         18&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 22:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646319#M193314</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-08T22:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646331#M193320</link>
      <description>SAS iterates over rows automatically and you manually need to loop over columns. In this case, it's usually easier to leave your data in a long format as its then easier to work with as it loops automatically. If this is time data and you're missing time points you can add that in using PROC TIMESERIES/EXPAND. There are options to impute missing as averages or last observation carried forward.&lt;BR /&gt;</description>
      <pubDate>Sat, 09 May 2020 00:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646331#M193320</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-09T00:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646369#M193333</link>
      <description>&lt;P&gt;Do you have SAS/IML ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input VAR1	VAR2	VAR3 ;
cards;
1	2	3
4	5	6
7	8	9
10	11	12
13	14	15
16	17	18
;

%let z=3;
proc iml;
use have;
read all var _num_ into x;
close;
z=shape(x,&amp;amp;z,0,.);
create want from z;
append from z;
close;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 May 2020 11:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646369#M193333</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-09T11:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate multiple rows into one row with additional variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646378#M193336</link>
      <description>&lt;P&gt;Presuming the original variable names are actually numerically suffixed 1..Y and the new columns use suffixes Y+1...Z*Y&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;the original names are for example Alpha, Beta, Grom, Zed, the new columns would have to be something like Alpha_2...Zed_2, Alpha_3...Zed_3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One approach is to write a macro to create a data step that uses arrays to pivot multiple rows into a single row.&amp;nbsp; Let's call it 'florping'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data generator for testing&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;%macro makedata(data,rows,cols,var);
  data &amp;amp;data;
    do r = 1 to &amp;amp;rows;
      do c = 1 to &amp;amp;cols;
        array &amp;amp;var(&amp;amp;cols);
        k + 1;
        &amp;amp;var(c) = k;
      end;
      output;
    end;
    drop r c k ;
  run;
%mend;

options mprint;

%makedata (one,10,3,zeta);
%makedata (two,30,24,zoink);
&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Florp macro&lt;/P&gt;
&lt;PRE&gt;%macro florp(data,var,count,out,format);

  %local N TOP; 

  proc transpose data=&amp;amp;data(obs=0) out=cols;
  proc sql noprint;
    select count(*) into :N trimmed from cols 
    where upcase(_name_) like %upcase("&amp;amp;var.%");

  %let TOP = %eval (&amp;amp;N * &amp;amp;count);

  data &amp;amp;out (drop=&amp;amp;var.: rename=(florp1-florp&amp;amp;TOP=&amp;amp;var.1-&amp;amp;var.&amp;amp;TOP));
    array florp (&amp;amp;TOP);
    retain _ix 0 florp;
    set &amp;amp;data end=done;
    array &amp;amp;var(&amp;amp;N);

    do _i_ = 1 to &amp;amp;N;
      _ix + 1;
      florp(_ix) = &amp;amp;var(_i_);
    end;

    if _ix = &amp;amp;TOP or done then do;
      OUTPUT;
      call missing (of florp(*));
      _ix = 0;
    end;
    drop _:;
    %if %length(&amp;amp;format) %then %do;
    format florp: &amp;amp;format.;
    %end;
  run;
%mend;
&lt;BR /&gt;%* count parameters is how many rows to combine into one;&lt;BR /&gt;%florp(data=one, var=zeta,  count=3, out=one_florped, format=4.);&lt;BR /&gt;%florp(data=two, var=zoink, count=7, out=two_florped, format=4.);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 May 2020 13:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-concatenate-multiple-rows-into-one-row-with-additional/m-p/646378#M193336</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-09T13:12:46Z</dc:date>
    </item>
  </channel>
</rss>

