<?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: Create variables each contains ordered values from existing variable within by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475366#M122238</link>
    <description>&lt;P&gt;Did my post not do this for you?&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jul 2018 07:35:10 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-07-04T07:35:10Z</dc:date>
    <item>
      <title>Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475104#M122151</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that contains such variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;time&lt;/TD&gt;&lt;TD&gt;drug&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2/1/2010&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3/1/2010&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;4/1/2010&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/1/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3/2/2010&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4/2/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;5/4/2010&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;6/4/2010&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;7/4/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;I wanted to create variables of drug1 drug2 drug3 drug4...etc which stores values of variable drug in a chronological order within per by group, so the output looks like:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;time&lt;/TD&gt;&lt;TD&gt;drug&lt;/TD&gt;&lt;TD&gt;drug1&lt;/TD&gt;&lt;TD&gt;drug2&lt;/TD&gt;&lt;TD&gt;drug3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2/1/2010&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;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;C&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;7/4/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can only think of the "sort, retain, carry down non-missing value" method. Is there a more efficient way to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 07:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475104#M122151</guid>
      <dc:creator>zzfsimona</dc:creator>
      <dc:date>2018-07-03T07:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475105#M122152</link>
      <description>&lt;P&gt;Sorry your going to have to explain that a bit clearer.&amp;nbsp; In the presented test data (which is not in a datastep!!!), there are four rows for id 1, are you saying that you want the sequence from all the id sorted, transposed and then merged back to the original data?&amp;nbsp; If so then - and note this is not tested as no test data in a datastep provided:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=list nodupkey;
  by id drug;
run;
proc tranpose data=list out=llist;
  by id;
  var drug;
run;
data want;
  merge have llist;
  by id;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Jul 2018 07:51:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475105#M122152</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-03T07:51:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475142#M122155</link>
      <description>&lt;P&gt;I think PROC TRANSPOSE is the right general direction, but I would make a few changes.&amp;nbsp; First, since you indicated chronological order, the sorting order would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by id time;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this assumes your TIME values are true SAS dates, not text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then get the variable names&amp;nbsp; you would like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=have prefix=drug out=druglist (drop=_name_);&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;var drug;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, merge as was suggested:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge have druglist;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 11:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475142#M122155</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-03T11:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475352#M122232</link>
      <description>&lt;P&gt;Thank you for the suggestion. What if I wanted the DISTINCT values transposed?&lt;/P&gt;&lt;P&gt;For example for id=1, now they have&lt;/P&gt;&lt;P&gt;drug1 drug2 drug3 drug4&lt;/P&gt;&lt;P&gt;A B B C&lt;/P&gt;&lt;P&gt;I want the distinct values so:&lt;/P&gt;&lt;P&gt;drug1 drug2 drug3&lt;/P&gt;&lt;P&gt;A B C&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 03:58:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475352#M122232</guid>
      <dc:creator>zzfsimona</dc:creator>
      <dc:date>2018-07-04T03:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475357#M122234</link>
      <description>&lt;P&gt;Untested: use proc sort with nodupkey option before transposing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=singles nodupkey;
  by id drug;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Wait .... that doesn't work because you need the dates of the eliminated duplicates. &lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 05:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475357#M122234</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-04T05:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475366#M122238</link>
      <description>&lt;P&gt;Did my post not do this for you?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 07:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475366#M122238</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-04T07:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create variables each contains ordered values from existing variable within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475469#M122276</link>
      <description>&lt;P&gt;Then you need to further define the outcome.&amp;nbsp; You asked for chronological order.&amp;nbsp; What if the chronological order is ABCB.&amp;nbsp; Do you want ABC as the result or ACB as the result?&amp;nbsp; That's just a simple example.&amp;nbsp; You really need a couple of comprehensive rules about what the order should be.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 16:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variables-each-contains-ordered-values-from-existing/m-p/475469#M122276</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-04T16:51:31Z</dc:date>
    </item>
  </channel>
</rss>

