<?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 Transpose in Data Step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921871#M363029</link>
    <description>&lt;P&gt;Due to the nature of how the data was collected, the data looks like this and is in txt file:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bone 1 ID&lt;/TD&gt;&lt;TD&gt;Bone 2 ID&lt;/TD&gt;&lt;TD&gt;Bone 3 ID&lt;/TD&gt;&lt;TD&gt;Bone 1 Type&lt;/TD&gt;&lt;TD&gt;Bone 2 Type&lt;/TD&gt;&lt;TD&gt;Bone 3 Type&lt;/TD&gt;&lt;TD&gt;Bone 1 Condition&lt;/TD&gt;&lt;TD&gt;Bone 2 Condition&lt;/TD&gt;&lt;TD&gt;Bone 3 Condition&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;156&lt;/TD&gt;&lt;TD&gt;182&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&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;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to read the txt file in and have it look like this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bone ID&lt;/TD&gt;&lt;TD&gt;Bone Type&lt;/TD&gt;&lt;TD&gt;Bone Condition&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;156&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;182&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know how I can achieve this. All variables are character variables. ID length should be $3. Bone type and bone condition should be a length of $1. Thank you!&lt;/P&gt;</description>
    <pubDate>Tue, 26 Mar 2024 21:30:49 GMT</pubDate>
    <dc:creator>jmontefalcon</dc:creator>
    <dc:date>2024-03-26T21:30:49Z</dc:date>
    <item>
      <title>Transpose in Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921871#M363029</link>
      <description>&lt;P&gt;Due to the nature of how the data was collected, the data looks like this and is in txt file:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bone 1 ID&lt;/TD&gt;&lt;TD&gt;Bone 2 ID&lt;/TD&gt;&lt;TD&gt;Bone 3 ID&lt;/TD&gt;&lt;TD&gt;Bone 1 Type&lt;/TD&gt;&lt;TD&gt;Bone 2 Type&lt;/TD&gt;&lt;TD&gt;Bone 3 Type&lt;/TD&gt;&lt;TD&gt;Bone 1 Condition&lt;/TD&gt;&lt;TD&gt;Bone 2 Condition&lt;/TD&gt;&lt;TD&gt;Bone 3 Condition&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;156&lt;/TD&gt;&lt;TD&gt;182&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&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;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to read the txt file in and have it look like this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bone ID&lt;/TD&gt;&lt;TD&gt;Bone Type&lt;/TD&gt;&lt;TD&gt;Bone Condition&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;156&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;182&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know how I can achieve this. All variables are character variables. ID length should be $3. Bone type and bone condition should be a length of $1. Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 21:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921871#M363029</guid>
      <dc:creator>jmontefalcon</dc:creator>
      <dc:date>2024-03-26T21:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose in Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921878#M363034</link>
      <description>&lt;P&gt;What you show for the source is not text, but a formatted display table of some flavor. Best would be to copy a few rows of data from the source and then paste it into a text box opened on the forum with the &amp;lt;/&amp;gt; icon that appears above the message box. The text box prevents reformatting of pasted text and pretty much shows what you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the text file delimited with some character like a comma, space or TAB between values or do the values appear in fixed column positions? This is important as the input statement needs information about that.&lt;/P&gt;
&lt;P&gt;Also are there &lt;STRONG&gt;always&lt;/STRONG&gt; the exact same number of items on every row?&lt;/P&gt;
&lt;P&gt;If some of your data rows look like:&lt;/P&gt;
&lt;TABLE border="1" width="572px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="70.9px"&gt;Bone 1 ID&lt;/TD&gt;
&lt;TD width="73.8833px"&gt;Bone 2 ID&lt;/TD&gt;
&lt;TD width="87.8167px"&gt;Bone 1 Type&lt;/TD&gt;
&lt;TD width="90.8px"&gt;Bone 2 Type&lt;/TD&gt;
&lt;TD width="122.8px"&gt;Bone 1 Condition&lt;/TD&gt;
&lt;TD width="124.8px"&gt;Bone 2 Condition&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="70.9px"&gt;123&lt;/TD&gt;
&lt;TD width="73.8833px"&gt;156&lt;/TD&gt;
&lt;TD width="87.8167px"&gt;0&lt;/TD&gt;
&lt;TD width="90.8px"&gt;1&lt;/TD&gt;
&lt;TD width="122.8px"&gt;1&lt;/TD&gt;
&lt;TD width="124.8px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;and others as shown this may be a moderately complicated bit. Which is why I suggested several rows (5 to 10 should do for basics).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example that will read a line that always has 3 sets of values.&lt;/P&gt;
&lt;PRE&gt;data example;
  length boneid $ 3 bonetype bonecondition $ 1;
  input @;
  do i=1 to 3;
     boneid= scan(_infile_,i);
     bonetype= scan(_infile_,i+3);
     bonecondition = scan(_infile_,i+5);
     output;
  end;
  drop i;
datalines;
123 156 182 0 1 0 1 2 3
;&lt;/PRE&gt;
&lt;P&gt;The input without any variables reads the line into a single automatic variable named _INFILE_ then a loop that expects to read exactly 3 values 3 times uses some arithmetic to determine the position in the _INFILE_ that a value should be and uses SCAN to extract. This particular example is going default to space as the delimiter though other characters such as comma would work as well (look up SCAN for the defaults).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However additional logic is needed if different numbers of sets of values are on a single line (and even more, if anything else appears).&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 22:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921878#M363034</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-26T22:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose in Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921890#M363045</link>
      <description>&lt;P&gt;That is not what a TEXT file looks like.&amp;nbsp; A text is lines of TEXT not a SPREADSHEET with grid lines drawn on the screen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So assuming you meant that the data looks like this (perhaps with a different delimiter character)&lt;/P&gt;
&lt;PRE&gt;Bone 1 ID|Bone 2 ID|Bone 3 ID|Bone 1 Type|Bone 2 Type|Bone 3 Type|Bone 1 Condition|Bone 2 Condition|Bone 3 Condition
123|156|182|0|1|0|1|2|3&lt;/PRE&gt;
&lt;P&gt;Then you could read it using something like this:&lt;/P&gt;
&lt;P&gt;Read the header row into a temporary array.&lt;/P&gt;
&lt;P&gt;For the other rows read each value and split the header part into the NAME part and BONE number part.&lt;/P&gt;
&lt;P&gt;Then re-order so the values for each BONE number are in order.&lt;/P&gt;
&lt;P&gt;Finally use PROC TRANSPOSE to convert it into a normal looking dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tall ;
  infile text dsd dlm='|' truncover length=ll column=cc;
  if _n_=1 then do;
    array headers[5000] $50 _temporary_;
    do cols=1 by 1 until(cc&amp;gt;ll);
      input headers[cols] @;
    end;
    retain cols;
    delete;
  end;
  row+1;
  do col=1 to cols;
    length bone 8 _name_ $32 ;
    _name_=scan(headers[col],-1,' ');
    bone=input(scan(headers[col],2,' '),32.);
    input value :$3. @;
    output;
  end;
  drop cols;
run;

proc print;
run;

proc sort;
  by row bone col;
run;

proc transpose data=tall out=wide(drop=_name_) ;
  by row bone;
  id _name_;
  var value;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&amp;nbsp; TALL&lt;/P&gt;
&lt;PRE&gt;Obs    row    col    bone    _name_       value

 1      1      1       1     ID            123
 2      1      2       2     ID            156
 3      1      3       3     ID            182
 4      1      4       1     Type          0
 5      1      5       2     Type          1
 6      1      6       3     Type          0
 7      1      7       1     Condition     1
 8      1      8       2     Condition     2
 9      1      9       3     Condition     3
&lt;/PRE&gt;
&lt;P&gt;WANT&lt;/P&gt;
&lt;PRE&gt;Obs    row    bone    ID     Type    Condition

 1      1       1     123     0          1
 2      1       2     156     1          2
 3      1       3     182     0          3

&lt;/PRE&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>Wed, 27 Mar 2024 01:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-in-Data-Step/m-p/921890#M363045</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-27T01:02:06Z</dc:date>
    </item>
  </channel>
</rss>

