<?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: Transposing using an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575586#M162845</link>
    <description>&lt;P&gt;Since the data is rotated you might need to read it into two arrays if you want to transpose it in one pass.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input label1 $ x1-x12 / label2 $ y1-y12 ;
   array x [12];
   array y [12];
   do subject=1 to 12 ;
       if label1 = 'Before' and label2='After' then do;
          before=x[subject]; after=y[subject];
      end;
       else if label2 = 'Before' and label1='After' then do;
          before=y[subject]; after=x[subject];
      end;
      else do;
          put 'Invalid row labels. ' label1= label2= ;
          stop;
      end;
      output;
   end;
   keep subject before after ;
cards;
Before 300 350 190 400 244 321 330 250 190 160 260 240
After 290 331 200 395 240 300 332 242 185 158 256 220
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 22 Jul 2019 20:43:33 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-07-22T20:43:33Z</dc:date>
    <item>
      <title>Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575575#M162837</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to transpose this set using version 9.4 so before and after are column headers and the datalines fall below it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before 300 350 190 400 244 321 330 250 190 160 260 240&lt;BR /&gt;After 290 331 200 395 240 300 332 242 185 158 256 220&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Every method I have tried thus far hasn't been successful. With the version of the code I've been currently using I get the error "Array subscript out of range at line ___"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What the heck am I missing? My current version of the code is below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA dietpair (keep = before after);&lt;BR /&gt;infile 'X:\SASDATA\cody_chap06_num06.dat';&lt;BR /&gt;input trt $ 1-6 subject1-subject12;&lt;BR /&gt;array weight (12) subject1-subject12;&lt;BR /&gt;do i = 1-12;&lt;BR /&gt;if trt = 'Before' then before = weight(i);&lt;BR /&gt;else if trt = 'After' then after = weight(i);&lt;BR /&gt;end;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:20:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575575#M162837</guid>
      <dc:creator>bonedog</dc:creator>
      <dc:date>2019-07-22T20:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575576#M162838</link>
      <description>&lt;P&gt;Negative eleven is not going to be a valid index into your array.&amp;nbsp; &amp;nbsp;You need use integers from 1 to 12 instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do I=1 to 12;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you only want the value for subject 12?&amp;nbsp; Or do you want to output all of them?&amp;nbsp; You need an OUTPUT statement inside your DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dietpair ;
  infile 'X:\SASDATA\cody_chap06_num06.dat';
  input trt $ 1-6 @ ;
  do subject=1 to 12;
     input weight @;
     output;
  end;
run;

proc sort; by subject trt; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575576#M162838</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-22T20:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575580#M162840</link>
      <description>&lt;P&gt;I don't know where you're seeing negative 11&amp;nbsp; - this is already what I have in my code.&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-inline" image-alt="Screen Shot 2019-07-22 at 4.25.01 PM.png" style="width: 431px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31203iEBD0CE9758484647/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-07-22 at 4.25.01 PM.png" alt="Screen Shot 2019-07-22 at 4.25.01 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575580#M162840</guid>
      <dc:creator>bonedog</dc:creator>
      <dc:date>2019-07-22T20:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575581#M162841</link>
      <description>&lt;P&gt;one minus twelve is negative eleven.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575581#M162841</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-22T20:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575583#M162842</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282450"&gt;@bonedog&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't know where you're seeing negative 11&amp;nbsp; - this is already what I have in my code.&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-inline" image-alt="Screen Shot 2019-07-22 at 4.25.01 PM.png" style="width: 431px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31203iEBD0CE9758484647/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-07-22 at 4.25.01 PM.png" alt="Screen Shot 2019-07-22 at 4.25.01 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Iterated do loops are&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do I = &amp;lt;startvalue&amp;gt; &lt;STRONG&gt;to&lt;/STRONG&gt; &amp;lt;endvalue&amp;gt; &amp;lt;optionally&amp;gt;&amp;nbsp;&lt;STRONG&gt;by&lt;/STRONG&gt; byvalue;&lt;/P&gt;
&lt;P&gt;Startvalue, endvalue and byvalue can be 1) literal values, 1 or 12, 2) variable names or 3) expressions that resolve to integers though the values and sign of byvalue need to matched carefully. Note the word is &lt;STRONG&gt;to, &lt;/STRONG&gt; not a dash.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575583#M162842</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-07-22T20:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575584#M162843</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for pointing out my error there. The fix has gotten me most of the way there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my current code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA dietpair (keep = before after);
	infile 'X:\SASDATA\cody_chap06_num06.dat';
	input trt $ 1-6 subject1-subject12;
	array weight (12) subject1-subject12;
	do i = 1 to 12;
	if trt = 'Before' then before = weight(i);
	else if trt = 'After' then after = weight(i);
	output;
	end;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And this is the output I'm getting.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;                             Obs    before    after

                               1      300        .
                               2      350        .
                               3      190        .
                               4      400        .
                               5      244        .
                               6      321        .
                               7      330        .
                               8      250        .
                               9      190        .
                              10      160        .
                              11      260        .
                              12      240        .
                              13        .      290
                              14        .      331
                              15        .      200
                              16        .      395
                              17        .      240
                              18        .      300
                              19        .      332
                              20        .      242
                              21        .      185
                              22        .      158
                              23        .      256
                              24        .      220


&lt;/PRE&gt;&lt;P&gt;How can I make it so I only have 12 observations where the before and after values line up?&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575584#M162843</guid>
      <dc:creator>bonedog</dc:creator>
      <dc:date>2019-07-22T20:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575585#M162844</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282450"&gt;@bonedog&lt;/a&gt;&amp;nbsp; &amp;nbsp;Do you just have two records in your source data?&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575585#M162844</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-07-22T20:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575586#M162845</link>
      <description>&lt;P&gt;Since the data is rotated you might need to read it into two arrays if you want to transpose it in one pass.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input label1 $ x1-x12 / label2 $ y1-y12 ;
   array x [12];
   array y [12];
   do subject=1 to 12 ;
       if label1 = 'Before' and label2='After' then do;
          before=x[subject]; after=y[subject];
      end;
       else if label2 = 'Before' and label1='After' then do;
          before=y[subject]; after=x[subject];
      end;
      else do;
          put 'Invalid row labels. ' label1= label2= ;
          stop;
      end;
      output;
   end;
   keep subject before after ;
cards;
Before 300 350 190 400 244 321 330 250 190 160 260 240
After 290 331 200 395 240 300 332 242 185 158 256 220
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 20:43:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-using-an-array/m-p/575586#M162845</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-22T20:43:33Z</dc:date>
    </item>
  </channel>
</rss>

