<?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: Generate new count variable based on existing variable in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949432#M371397</link>
    <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; - this is precisely what I was looking for here! Just in response to your query, since I'm not counting fruit by ID, apple-orange count range is 1-4 -- sorry, I probably could have explained this better in my original post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you again for your help!&lt;/P&gt;</description>
    <pubDate>Tue, 29 Oct 2024 22:49:48 GMT</pubDate>
    <dc:creator>Epi_Stats</dc:creator>
    <dc:date>2024-10-29T22:49:48Z</dc:date>
    <item>
      <title>Generate new count variable based on existing variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949416#M371395</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large dataset with a summary of each respondent’s favourite fruit combination at different times of the day (breakfast, lunch, dinner):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID time $ fruit :$30.;
datalines; 
1 bfast apple-orange
1 bfast apple-orange
1 dinner apple-orange
2 bfast pear-peach
2 dinner pear-peach
3 bfast banana-mango
3 lunch banana-mango
3 lunch banana-mango
4 lunch kiwi-guava
4 dinner kiwi-guava
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a new variable “count” based on each fruit combination – I include an example output below of what I want. This variable does NOT need to be based on the unique ID – for example, apple-orange would have a count range 1-4, even though ID 1 and 2 share this combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID time $ fruit :$30. count;
datalines; 
1 bfast apple-orange 1
1 bfast apple-orange 2
1 dinner apple-orange 3
2 bfast apple-orange 4
2 dinner pear-peach 1
3 bfast banana-mango 1
3 lunch banana-mango 2
3 lunch banana-mango 3
4 lunch kiwi-guava 1
4 dinner kiwi-guava 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought I could use a do loop to achieve this, but I’m struggling to think how to do this in SAS, and would be very grateful for any suggestions/help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 21:10:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949416#M371395</guid>
      <dc:creator>Epi_Stats</dc:creator>
      <dc:date>2024-10-29T21:10:08Z</dc:date>
    </item>
    <item>
      <title>Re: Generate new count variable based on existing variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949427#M371396</link>
      <description>&lt;P&gt;I am confused by your problem statement. I don't see how apple-orange gets a "count range" of 1 to 4 from the original data, when apple-orange appears 3 times. Please explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if I may guess, you can sort the data by the fruit and then obtain a count like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by fruit;
run;

data want;
    set have;
    by fruit;
    if first.fruit then count=0;
    count+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 22:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949427#M371396</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-29T22:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Generate new count variable based on existing variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949432#M371397</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; - this is precisely what I was looking for here! Just in response to your query, since I'm not counting fruit by ID, apple-orange count range is 1-4 -- sorry, I probably could have explained this better in my original post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you again for your help!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 22:49:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949432#M371397</guid>
      <dc:creator>Epi_Stats</dc:creator>
      <dc:date>2024-10-29T22:49:48Z</dc:date>
    </item>
    <item>
      <title>Re: Generate new count variable based on existing variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949455#M371402</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442273"&gt;@Epi_Stats&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; - this is precisely what I was looking for here! Just in response to your query, since I'm not counting fruit by ID, apple-orange count range is 1-4 -- sorry, I probably could have explained this better in my original post.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;While I am glad to solve your problem, this explanation explains nothing. There are not 4 apple-orange in your data. When you provide a desired answer, it should be consistent with the input data you provide. Providing desired output that is consistent with the input data on future questions will help you get faster and better answers. You got lucky on this question, I guessed right as to what you wanted, but I (and others) don't always guess right.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 09:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-count-variable-based-on-existing-variable-in/m-p/949455#M371402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-30T09:49:06Z</dc:date>
    </item>
  </channel>
</rss>

