<?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: HELP NEEDED ASAP: Counting consecutive variable values in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278146#M58836</link>
    <description>&lt;P&gt;First - nothing on this community can be ASAP. All entries are voluntary.&lt;/P&gt;
&lt;P&gt;Second - if you figured out how to do it - share! Some om your peers can benefit&amp;nbsp;from this. And it's nice to couple a solution to your question.&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2016 10:42:43 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-06-17T10:42:43Z</dc:date>
    <item>
      <title>Counting consecutive variable values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/277981#M58798</link>
      <description>&lt;P&gt;Hello Fellow SAS Users,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have been working on something for a few weeks and just ran into a problem with my code. Esentially, I need SAS to count the number of consecutive variable values (including restarting values) separately for each participant.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(I am not going to include my current SAS code, because I am sure it is very inefficient and there is a much better way to achieve this.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two problems, but first take&amp;nbsp;a look at&amp;nbsp;an example of the output:&lt;/P&gt;
&lt;P&gt;NAME = Participant&lt;/P&gt;
&lt;P&gt;DSCD = Identifier&lt;/P&gt;
&lt;P&gt;FYEAR = Year of observation&lt;/P&gt;
&lt;P&gt;AUD = the variable for which I need to count the number of repititions&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TENURE = the count of the repition of AUD, as of the current FYEAR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/3653iE5005BE076FA134C/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="output problem.png" title="output problem.png" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My two problems are summarized as follows (based no the two &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;red boxes&lt;/STRONG&gt;&lt;/FONT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. First, the very first observation for each participant should be where TENURE=1. My code is showing as TENURE=. and then starting TENURE=1 in the second observation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. The more important problem is what happens in the second participant. The same problem as #1 above persists. But then in FYEAR=1996 for this participant, it is continuing off of the previous count string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yet, interestingly, for both participants shown here (look at the &lt;STRONG&gt;&lt;FONT color="#00CCFF"&gt;BLUE boxes&lt;/FONT&gt;&lt;/STRONG&gt;, FYEAR=2002 for the first and FYEAR=2007 for the second), when there is a change in the AUD variable, neither of the two preceding problems are present. The first year of the new AUD variable is = 1, and it restarts the count rather than pulling from the previous count.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate any help with this. Again, I'm sorry for not including my current SAS code but I am very confident that it is wasteful and inefficient.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 10:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/277981#M58798</guid>
      <dc:creator>wriccar</dc:creator>
      <dc:date>2016-06-17T10:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: Counting consecutive variable values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278000#M58800</link>
      <description>&lt;P&gt;Update: I am trying to create a simple DO loop and this is also giving me problems. Here's the code I'm working with for that:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*COMPUTE TENURE (IN YEARS) AND CLASSIFY AS S/M/L*;
data tenure; set merged;
aud=aud2;
dscd1 = lag(dscd);
aud1 = lag(aud);

if dscd ne dscd1 then tenure=1; 
else if dscd=dscd1 then do;
tenure1=lag(tenure);
if aud=aud1 then tenure = tenure1+1; 
if aud ne aud1 then tenure = 1; 
end; 

if tenure &amp;lt;=3 then tenure_c='S';
else if 7 &amp;gt;= tenure &amp;gt; 3 then tenure_c='M';
else if tenure &amp;gt; 7 then tenure_c='L';
else if tenure='.' then tenure_c='.';

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jun 2016 18:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278000#M58800</guid>
      <dc:creator>wriccar</dc:creator>
      <dc:date>2016-06-16T18:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: Counting consecutive variable values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278014#M58804</link>
      <description>&lt;P&gt;Figured it out, please do not reply.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 19:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278014#M58804</guid>
      <dc:creator>wriccar</dc:creator>
      <dc:date>2016-06-16T19:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: Counting consecutive variable values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278146#M58836</link>
      <description>&lt;P&gt;First - nothing on this community can be ASAP. All entries are voluntary.&lt;/P&gt;
&lt;P&gt;Second - if you figured out how to do it - share! Some om your peers can benefit&amp;nbsp;from this. And it's nice to couple a solution to your question.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2016 10:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/278146#M58836</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-06-17T10:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: HELP NEEDED ASAP: Counting consecutive variable values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/612458#M76839</link>
      <description>&lt;P&gt;I realize this post is 3 years-old (and the original poster had already resolved the issue him/herself), but I have found myself in a similar situation, stumbling across a SAS community question that perfectly replicates the question I'm trying to sort out, with no final resolution. Just in case others are struggling to find syntax to create a count variable, here is some code to help:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, sort data by your grouping variable and the variable you want counted.&amp;nbsp;ID = grouping variable; Date = ordering variable&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=one;
	by ID Date;
	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Next, create a count variable in a new data set. In this case, each first ID starts at a count of 1, and continues to count up until the next ID.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
	set one; 
	*Create count variable.;
	Count + 1;
	by ID;
	if first.ID then Count = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you wanted to create a running count within more than one category, for example, number of observations within each participant and date, you can simply add the additional variable to your count syntax after the "by" statement (e.g., "by ID Date").&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you need something slightly more complicated, for example, counting only certain types of observations, some additional syntax is listed below.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
	set one; 
	*Create drinking observation count.;
	if Drinking=1 then Count + 1;
	by ID;
	if first.ID then Count = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here, I created a count of drinking observations (only when drinking = 1, not drinking = 0) within each participant ID.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 17:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-consecutive-variable-values/m-p/612458#M76839</guid>
      <dc:creator>cmo5</dc:creator>
      <dc:date>2019-12-17T17:00:37Z</dc:date>
    </item>
  </channel>
</rss>

