<?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: PROC TRANSPOSE with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272368#M54179</link>
    <description>&lt;P&gt;Single data step version which accomodates missing visit_codes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do i = 0 by 1 until(last.patient_id and mod(i,3) = 2);
    if not (i and last.patient_id) then do;
        set have; by patient_id;
        array var_{0:2} $8;
        var_{mod(i,3)} = visit_code;
        end;
    else call missing(var_{mod(i,3)});
    if missing(var_{mod(i,3)}) then var_{mod(i,3)} = "NOTUSED";
    if mod(i,3) = 2 then output;
    end;
drop i visit_code;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 May 2016 05:10:21 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-05-23T05:10:21Z</dc:date>
    <item>
      <title>PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272353#M54174</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;My following PROC TRANSPOSE codes are working, but I want:&lt;/P&gt;
&lt;P&gt;1. Maximum three columns and if there are more than three variables I want it in the second line. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Any additional space should be filled up with 'NOTUSED'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can somebody help please.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; input patient_ID:$3. Visit_code $ 6.; &lt;BR /&gt; cards; &lt;BR /&gt;101 LP28M72&lt;BR /&gt;101 LP23M66 &lt;BR /&gt;101 LP22M64&lt;BR /&gt;101 LP29M64&lt;BR /&gt;102 SR66F76 &lt;BR /&gt;102 SR62F76 &lt;BR /&gt;102 SR61F76 &lt;BR /&gt;102 SR69F76 &lt;BR /&gt;102 SR77F76 &lt;BR /&gt;103 JH23F56 &lt;BR /&gt;103 JH43F56 &lt;BR /&gt;;&lt;BR /&gt; run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc transpose data=work.have out=work.want (drop=_name_) prefix=Var_;&lt;BR /&gt; by patient_ID;&lt;BR /&gt; var Visit_code;&lt;BR /&gt; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Expected output:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;patient_ID Var_1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Var_2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Var_3 &lt;BR /&gt;101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LP28M72 &amp;nbsp; LP23M66 &amp;nbsp; LP22M64&lt;BR /&gt;101 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LP29M64 &amp;nbsp; NOTUSED NOTUSED&lt;BR /&gt;102 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SR66F76 &amp;nbsp; SR62F76 &amp;nbsp; SR61F76 &lt;BR /&gt;102 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SR69F76 &amp;nbsp; SR77F76 &amp;nbsp; NOTUSED &lt;BR /&gt;103 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; JH23F56 &amp;nbsp; JH43F56 &amp;nbsp; NOTUSED &lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 03:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272353#M54174</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-05-23T03:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272357#M54175</link>
      <description>&lt;P&gt;1. Create a line number and increment when it's pass 3 to control the lines&lt;/P&gt;
&lt;P&gt;2. Transpose&lt;/P&gt;
&lt;P&gt;3. Loop over using an array and set to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input patient_ID:$3. Visit_code $ 6.; 
cards; 
101 LP28M72
101 LP23M66 
101 LP22M64
101 LP29M64
102 SR66F76 
102 SR62F76 
102 SR61F76 
102 SR69F76 
102 SR77F76 
103 JH23F56 
103 JH43F56 
;
run;

data have2;
set have;
by patient_id;
if first.id then count=1;
else count+1;
line=floor((count-0.5)/3);
drop count;
run;

proc transpose data=work.have2 out=work.want (drop=_name_) prefix=Var_;
by patient_ID line;
var Visit_code;
run;

data want2;
set want;
array var(3) var_1-var_3;
do i=1 to dim(var);
if missing(var(i)) then var(i)="No Status";
end;
run;

proc print;run;
 
 

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 03:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272357#M54175</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-23T03:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272359#M54176</link>
      <description>&lt;P&gt;Well, you can't do the job without a Data step (I don't think), so forget about the Proc Transpose and do the whole thing in&amp;nbsp;1 step.&lt;/P&gt;&lt;P&gt;Something like this works...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop= i visit_code Visit);
  set have;
  by patient_id;
  array v(3) $7 var_1 - var_3;
  retain visit 0  var_1-var_3 '';

  if first.patient_id then
	visit=0;
  visit+1;
  if visit&amp;gt;3 then visit=1;
  v{visit} = visit_code;
  if last.patient_id 
  or  mod(visit,3) = 0 then
    do;
	 do i=visit+1 to 3;
	   v{i} = 'NOTUSED';
	 end;
         output;
    end;  
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 04:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272359#M54176</guid>
      <dc:creator>JerryLeBreton</dc:creator>
      <dc:date>2016-05-23T04:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272360#M54177</link>
      <description>&lt;P&gt;Same idea as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;, 1 less step&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient_ID:$3. Visit_code $ 6.; 
cards; 
101 LP28M72
101 LP23M66 
101 LP22M64
101 LP29M64
102 SR66F76 
102 SR62F76 
102 SR61F76 
102 SR69F76 
102 SR77F76 
103 JH23F56 
103 JH43F56 
;

data have2;
do i = 1 by 1 until(last.patient_id and mod(i,3) = 0);
    line = floor((i-1)/3);
    if i &amp;gt; 1 and last.patient_id then Visit_code = "NOTUSED";
    else do;
        set have; by patient_id;
        end;
    output;
    end;
drop i;
run;

proc transpose data=have2 out=want(drop=_name_ line) prefix=var_;
by patient_id line;
var visit_code;
run;

proc print noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 04:20:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272360#M54177</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-05-23T04:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272363#M54178</link>
      <description>&lt;P&gt;That is pretty easy to do with an ARRAY and a DOW loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do _n_=1 by 1 until (last.pid or mod(_n_,3)=0) ;
    set have ;
    by pid;
    array col (3) $8 ;
    col(_n_)=visit;
  end;
  drop visit ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want to have the missing values replaced with 'NOT USED' then add another DO loop before the RUN statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do _n_=_n_+1 to 3;
    col(_n_)='NOT USED';
  end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 04:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272363#M54178</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-23T04:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272368#M54179</link>
      <description>&lt;P&gt;Single data step version which accomodates missing visit_codes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do i = 0 by 1 until(last.patient_id and mod(i,3) = 2);
    if not (i and last.patient_id) then do;
        set have; by patient_id;
        array var_{0:2} $8;
        var_{mod(i,3)} = visit_code;
        end;
    else call missing(var_{mod(i,3)});
    if missing(var_{mod(i,3)}) then var_{mod(i,3)} = "NOTUSED";
    if mod(i,3) = 2 then output;
    end;
drop i visit_code;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 05:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272368#M54179</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-05-23T05:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272473#M54200</link>
      <description>Hi Reeza,&lt;BR /&gt;By group 102 does not seem right with your code. would you please modify the code. thanks.</description>
      <pubDate>Mon, 23 May 2016 17:16:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272473#M54200</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-05-23T17:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272507#M54213</link>
      <description>&lt;P&gt;Change the First.ID to the correct variable, First.Patient_ID&lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 18:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272507#M54213</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-23T18:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TRANSPOSE with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272577#M54246</link>
      <description>&lt;P&gt;Why use proc transpose?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input PATIENT_ID:$3. VISIT_CODE $ 7.; 
 cards; 
101 LP28M72
101 LP23M66 
101 LP22M64
101 LP29M64
102 SR66F76 
102 SR62F76 
102 SR61F76 
102 SR69F76 
102 SR77F76 
103 JH23F56 
103 JH43F56 
run;
data WANT;
  array VAR [3] $7;
  retain VAR:;
  set HAVE;
  by PATIENT_ID;
  keep PATIENT_ID VAR:;
  if first.PATIENT_ID | VISIT_NB=3 then VISIT_NB=0;
  VISIT_NB+1;
  VAR[VISIT_NB]=VISIT_CODE;
  if last.PATIENT_ID | VISIT_NB=3 then do;
    do while (VISIT_NB &amp;lt; 3);
      VISIT_NB+1;
      VAR[VISIT_NB]='UNUSED';
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 22:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-TRANSPOSE-with-conditions/m-p/272577#M54246</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-05-23T22:32:08Z</dc:date>
    </item>
  </channel>
</rss>

