<?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 transposing variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50740#M10602</link>
    <description>Phy_Name	Total	Drugname&lt;BR /&gt;
Bruce FREEDMAN	240.6	Paracetemol&lt;BR /&gt;
Bruce FREEDMAN	15085	Bendex&lt;BR /&gt;
Bruce FREEDMAN	4190	natropin&lt;BR /&gt;
Bruce FREEDMAN	1086.6	Saisen&lt;BR /&gt;
Bruce RICHTON	924.6	Paracetemol&lt;BR /&gt;
Bruce RICHTON	672	Humasona&lt;BR /&gt;
Bruce RICHTON	270	Bendex&lt;BR /&gt;
Bruce RICHTON	170	natropin&lt;BR /&gt;
Bruce RICHTON	1144	Saisen&lt;BR /&gt;
&lt;BR /&gt;
For the above data, I want to transpose the drugname and total so that each physician will have a single record.&lt;BR /&gt;
&lt;BR /&gt;
I tried the following code:&lt;BR /&gt;
The total is being mismatched with the drugnames.i.e it is getting displaced by one position to the left if the correspoding drug name is missing.&lt;BR /&gt;
for instance: Phyname   Bruce FREEDMAN  doesnt have humasona, it should give us a missing instead.but the value of saisen is given a missing and its value is displced by one position to the left.        &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
  data new(keep=Phy_Name Paracetemol Saisen natropin Bendex Humasona) ;&lt;BR /&gt;
  set allphysicians;&lt;BR /&gt;
       by Phy_Name ;&lt;BR /&gt;
    array  drg {&amp;amp;n} Paracetemol Saisen natropin Bendex Humasona ;&lt;BR /&gt;
	retain Paracetemol Saisen natropin Bendex Humasona;&lt;BR /&gt;
     if first.Phy_Name then i=1;&lt;BR /&gt;
	 else i + 1;&lt;BR /&gt;
           drg{i} = var1_tot;&lt;BR /&gt;
		   if last.Phy_Name;&lt;BR /&gt;
		   if i &amp;lt; &amp;amp;n then do i=i+1 to &amp;amp;n;&lt;BR /&gt;
		   drg{i} =.;&lt;BR /&gt;
    end;&lt;BR /&gt;
  run;</description>
    <pubDate>Wed, 01 Jul 2009 19:38:58 GMT</pubDate>
    <dc:creator>SASPhile</dc:creator>
    <dc:date>2009-07-01T19:38:58Z</dc:date>
    <item>
      <title>transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50740#M10602</link>
      <description>Phy_Name	Total	Drugname&lt;BR /&gt;
Bruce FREEDMAN	240.6	Paracetemol&lt;BR /&gt;
Bruce FREEDMAN	15085	Bendex&lt;BR /&gt;
Bruce FREEDMAN	4190	natropin&lt;BR /&gt;
Bruce FREEDMAN	1086.6	Saisen&lt;BR /&gt;
Bruce RICHTON	924.6	Paracetemol&lt;BR /&gt;
Bruce RICHTON	672	Humasona&lt;BR /&gt;
Bruce RICHTON	270	Bendex&lt;BR /&gt;
Bruce RICHTON	170	natropin&lt;BR /&gt;
Bruce RICHTON	1144	Saisen&lt;BR /&gt;
&lt;BR /&gt;
For the above data, I want to transpose the drugname and total so that each physician will have a single record.&lt;BR /&gt;
&lt;BR /&gt;
I tried the following code:&lt;BR /&gt;
The total is being mismatched with the drugnames.i.e it is getting displaced by one position to the left if the correspoding drug name is missing.&lt;BR /&gt;
for instance: Phyname   Bruce FREEDMAN  doesnt have humasona, it should give us a missing instead.but the value of saisen is given a missing and its value is displced by one position to the left.        &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
  data new(keep=Phy_Name Paracetemol Saisen natropin Bendex Humasona) ;&lt;BR /&gt;
  set allphysicians;&lt;BR /&gt;
       by Phy_Name ;&lt;BR /&gt;
    array  drg {&amp;amp;n} Paracetemol Saisen natropin Bendex Humasona ;&lt;BR /&gt;
	retain Paracetemol Saisen natropin Bendex Humasona;&lt;BR /&gt;
     if first.Phy_Name then i=1;&lt;BR /&gt;
	 else i + 1;&lt;BR /&gt;
           drg{i} = var1_tot;&lt;BR /&gt;
		   if last.Phy_Name;&lt;BR /&gt;
		   if i &amp;lt; &amp;amp;n then do i=i+1 to &amp;amp;n;&lt;BR /&gt;
		   drg{i} =.;&lt;BR /&gt;
    end;&lt;BR /&gt;
  run;</description>
      <pubDate>Wed, 01 Jul 2009 19:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50740#M10602</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-07-01T19:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50741#M10603</link>
      <description>Did you look at PROC TRANSPOSE?&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 01 Jul 2009 19:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50741#M10603</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-01T19:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50742#M10604</link>
      <description>I tried proc tranpose, but there were issues with duplicate id values.</description>
      <pubDate>Wed, 01 Jul 2009 19:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50742#M10604</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-07-01T19:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50743#M10605</link>
      <description>And your DATA step (very manual intense) approach can address them?  Maybe if you explained the problem, there may be a method to still use PROC TRANSPOSE??&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 01 Jul 2009 19:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50743#M10605</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-01T19:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50744#M10606</link>
      <description>Hello SASPhile,&lt;BR /&gt;
&lt;BR /&gt;
Scott's proposal is the correct one. Perhaeps you should aggregate your data before using the proc transpose. The program could be:&lt;BR /&gt;
&lt;BR /&gt;
data T01_data;&lt;BR /&gt;
infile cards delimiter='|';&lt;BR /&gt;
length Phy_Name $30 Drugname $30;&lt;BR /&gt;
input Phy_Name Total Drugname;&lt;BR /&gt;
cards;&lt;BR /&gt;
Bruce FREEDMAN|240.6|Paracetemol&lt;BR /&gt;
Bruce FREEDMAN|15085|Bendex&lt;BR /&gt;
Bruce FREEDMAN|4190|natropin&lt;BR /&gt;
Bruce FREEDMAN|1086.6|Saisen&lt;BR /&gt;
Bruce RICHTON|924.6|Paracetemol&lt;BR /&gt;
Bruce RICHTON|672|Humasona&lt;BR /&gt;
Bruce RICHTON|270|Bendex&lt;BR /&gt;
Bruce RICHTON|170|natropin&lt;BR /&gt;
Bruce RICHTON|1144|Saisen&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc means data=T01_data NWAY NOPRINT MISSING ;&lt;BR /&gt;
class phy_name drugname;&lt;BR /&gt;
var total;&lt;BR /&gt;
output out=T02_aggregated sum(total)=sum_of_total;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=T02_aggregated out=T03_transposed(drop=_NAME_);&lt;BR /&gt;
by phy_name;&lt;BR /&gt;
id drugname;&lt;BR /&gt;
var sum_of_total;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Does it solve the problem?&lt;BR /&gt;
&lt;BR /&gt;
Yoba</description>
      <pubDate>Wed, 01 Jul 2009 19:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50744#M10606</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-07-01T19:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: transposing variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50745#M10607</link>
      <description>I think the data step fails because it assumes it will find all drugnames in your array, but the first physician does not deal with Humasona.&lt;BR /&gt;
Transpose should do[pre]proc sort data= ph out= phy ;&lt;BR /&gt;
  by phy_name drugname ;&lt;BR /&gt;
run ;&lt;BR /&gt;
proc transpose  data= phy out =phyt  ;&lt;BR /&gt;
   by Phy_Name;&lt;BR /&gt;
   id drugname ;&lt;BR /&gt;
   var total ;&lt;BR /&gt;
run ;[/pre]&lt;BR /&gt;
As should a data step.&lt;BR /&gt;
When I had to make a data step do this kind of transpose, I found it helpful to create a user informat to translate the drugName to an array entry pointer. To reduce the code for your simple data, below, I place a drugName every 12th position in a string and calculate the array index entry with a formula based on the indexW() function applied to the &amp;amp;DrugList.[pre]%let drugList = &lt;BR /&gt;
Paracetemol Humasona    Bendex      natropin    Saisen  ;&lt;BR /&gt;
data phyT_ds ;&lt;BR /&gt;
  keep   phy_name   &amp;amp;drugList ;&lt;BR /&gt;
  do until( last.phy_name ) ;&lt;BR /&gt;
      set phy;&lt;BR /&gt;
      by  phy_name ;&lt;BR /&gt;
      array dr(*)    &amp;amp;drugList  ;&lt;BR /&gt;
      idx = indexw( "&amp;amp;drugList", trim( drugname ) ) / 12 +1 ;&lt;BR /&gt;
      dr( idx ) = sum( dr( idx ), total ) ;&lt;BR /&gt;
   end ;&lt;BR /&gt;
run ;&lt;BR /&gt;
proc print ;&lt;BR /&gt;
run ;[/pre]&lt;BR /&gt;
works for me &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Thu, 02 Jul 2009 14:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/transposing-variables/m-p/50745#M10607</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-07-02T14:23:22Z</dc:date>
    </item>
  </channel>
</rss>

