<?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 SQL - Counting by ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417778#M102621</link>
    <description>&lt;P&gt;Do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data numbered;
set have;
by mrn;
if first.mrn
then time = 1;
else time + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on how your dataset is ordered with regards to mrn, you will have to either sort first or use the notsorted option in the by statement.&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2017 16:02:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-12-01T16:02:02Z</dc:date>
    <item>
      <title>PROC SQL - Counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417776#M102620</link>
      <description>&lt;P&gt;I have this hypothetical dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;MRN     Height      Weight 
12345    65           22
12345    62           22
77111    60           33
77111    70           28
77111    58           35&lt;/PRE&gt;&lt;P&gt;Ultimately, I want to convert from long to wide, with variables like Height1, Height2, Weight1, Weight2, etc. using PROC TRANSPOSE. However, I need to create a "time" variable that indicates when the height and weight were measured before I can do this. I was thinking of counting the number of rows by MRN and adding a new column that goes from 1 to the number of rows by MRN. I need the dataset to look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;MRN     Height      Weight       Time
12345    65           22          1
12345    62           22          2
77111    60           33          1
77111    70           28          2
77111    58           35          3&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 15:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417776#M102620</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-12-01T15:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417778#M102621</link>
      <description>&lt;P&gt;Do it in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data numbered;
set have;
by mrn;
if first.mrn
then time = 1;
else time + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on how your dataset is ordered with regards to mrn, you will have to either sort first or use the notsorted option in the by statement.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417778#M102621</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-01T16:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417781#M102622</link>
      <description>This is perfect. Thanks so much.</description>
      <pubDate>Fri, 01 Dec 2017 16:06:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417781#M102622</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-12-01T16:06:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417786#M102624</link>
      <description>&lt;P&gt;Is there a reason you want to denormalize your data?&amp;nbsp; It will make it harder to work with.&amp;nbsp; If you want to find things like difference in weight over time I guarantee the format it is in now (height, weight, time) will be a lot easier to work with than weight1, time1, weight2, time2, weight3, time3, weight4, time4, etc. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417786#M102624</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-12-01T16:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417791#M102625</link>
      <description>&lt;P&gt;Its rarely a good idea to transpose data, it makes it harder to work with the data.&amp;nbsp; Anyhows, you don't need time, or tranpose, you can simply do it with arrays (I fix the number of elements here to 3, but you could take a max and use that rather):&lt;/P&gt;
&lt;PRE&gt;data want (keep=mrn height: weight:);
  set have (rename=(height=_height weight=_weight));
  array height{3};
  array weight{3};
  by mrn;
  retain ind;
  if first.mrn then ind=1;
  else ind=ind+1;
  height{ind}=_height;
  weight{ind}=_weight;
  if last.mrn then output;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Counting-by-ID/m-p/417791#M102625</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-01T16:19:22Z</dc:date>
    </item>
  </channel>
</rss>

