Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Count the number of changing string values


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Count the number of changing string values
Date   Fri, 14 Jun 2013 08:27:13 -0400

My comments in the earlier posting didn't get through. The code I
pasted earlier counts the switches between yes and no, either way. You
can use it as a basis for writing the code that solves your particular
task. It is rather easy to tweak the if-condition in the replace
statement to accommodate various scenarios, such as switching from
yes-to-no only or switching only once.
Best, Sergiy

On Fri, Jun 14, 2013 at 8:21 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> Fine. If we
>
> tsset id year
>
> and guess at labels  1 No 2 Yes
>
> then Yes after No the previous year is
>
> gen NY = drug == 2 & L.drug == 1
>
> and years of NY are
>
> gen yearNY = year if NY
>
> Yes after No in _any_ previous year can be done too.
>
> Nick
> njcoxstata@gmail.com
>
>
> On 14 June 2013 13:03, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote:
>> The long dataset is as follows:
>>
>> id sex year drug
>> 1   F   1999   Yes
>> 1   F   2000   No
>> 1   F   2001   Yes
>> 1   F   2002     .
>> 3   M  1999    No
>> 3   M  2000    Yes
>> 3   M  2001    Yes
>> 3   M  2002    No
>> 5   F   1999    Yes
>> 5   F   2000    Yes
>> 5   F   2001    Yes
>> 5   F   2002    Yes
>> 10 M   1999    Yes
>> 10 M   2000    No
>> 10 M   2001    Yes
>> 10 M   2002    No
>>
>> Thanks.
>>
>> Ching
>>
>> On 14 June 2013 21:20, Nick Cox <njcoxstata@gmail.com> wrote:
>>> Fine. If you tell me what your data are  once -reshape-d I will advise.
>>>
>>> Nick
>>> njcoxstata@gmail.com
>>>
>>>
>>> On 14 June 2013 12:45, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote:
>>>> Hi Nick,
>>>>
>>>> I would like to count "how many times a user said Yes one year and No the next".
>>>>
>>>> Thanks.
>>>>
>>>> Ching
>>>>
>>>> On 14 June 2013 21:11, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>> I take this to be a new question, but I am unclear what you want. Do you want
>>>>>
>>>>> 1. Counting how many times a user said Yes one year and No the next?
>>>>>
>>>>> 2. Identifying the first year in which a user said Yes after
>>>>> previously saying No.
>>>>>
>>>>> I fall back on emphasising my previous advice
>>>>>
>>>>> Long structure ... is likely to be better for you for most analyses.
>>>>>
>>>>> Get these data -reshape-d. You are going to want to do lots of
>>>>> analyses on different kinds of patterns, and keeping this structure is
>>>>> really going to strain any user's Stata fluency.
>>>>>
>>>>> Nick
>>>>> njcoxstata@gmail.com
>>>>>
>>>>>
>>>>> On 14 June 2013 12:12, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote:
>>>>>> Thanks Nick
>>>>>>
>>>>>> Yes, it's all good if I only need to count the first "NO'.
>>>>>>
>>>>>> But I would rather to see how to count the first "YES & NO" between
>>>>>> year of study.
>>>>>>
>>>>>> id Drug2009   Drug 2000
>>>>>> 1    Yes              NO      ------->ONE count
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Ching
>>>>>>
>>>>>> On 14 June 2013 20:24, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>>> I can't see your value labels from here. But suppose that "No" is a
>>>>>>> label for value 1.
>>>>>>>
>>>>>>> replace year = `j' if  Drug`j' == 1 & missing(year)
>>>>>>>
>>>>>>> I trust that you will be able to modify that to your situation.
>>>>>>>
>>>>>>>
>>>>>>> Nick
>>>>>>> njcoxstata@gmail.com
>>>>>>>
>>>>>>>
>>>>>>> On 14 June 2013 11:45, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote:
>>>>>>>> Hi Nick,
>>>>>>>>
>>>>>>>> So what if -drug*- are numeric variables with value labels, how should
>>>>>>>> I change my Command?
>>>>>>>>
>>>>>>>>
>>>>>>>> Thanks!
>>>>>>>>
>>>>>>>>
>>>>>>>> Ching
>>>>>>>>
>>>>>>>> On 14 June 2013 19:19, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>>>>> Code that "doesn't work" but is not shown can not be commented on.
>>>>>>>>> Long structure (I prefer to avoid the word "format" here) is not
>>>>>>>>> essential for the problem as I understand it, but it is likely to be
>>>>>>>>> better for you for most analyses.
>>>>>>>>>
>>>>>>>>> The year in which people first said "No" is
>>>>>>>>>
>>>>>>>>> gen year = .
>>>>>>>>>
>>>>>>>>> qui forval j = 1999/2012 {
>>>>>>>>>         replace year = `j' if  Drug`j' == "No" & missing(year)
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> tab year
>>>>>>>>>
>>>>>>>>> So,
>>>>>>>>>
>>>>>>>>> 1. Everybody starts out with missing.
>>>>>>>>>
>>>>>>>>> 2. If they said "No" in 1999, -year- is changed to 1999
>>>>>>>>>
>>>>>>>>> 3. If they said "No" in 2000, -year- is changed to 2000, but only if
>>>>>>>>> -year- is missing (so values of 1999 are protected)
>>>>>>>>>
>>>>>>>>> and so on.
>>>>>>>>>
>>>>>>>>> You didn't explain your datatypes. If -drug*- are numeric variables
>>>>>>>>> with value labels, the syntax will differ accordingly.
>>>>>>>>>
>>>>>>>>> Nick
>>>>>>>>> njcoxstata@gmail.com
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On 14 June 2013 10:20, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote:
>>>>>>>>>
>>>>>>>>>> I have the following data set:
>>>>>>>>>>
>>>>>>>>>> Id     sex   drug1999   drug2000    drug2001      drug 2001
>>>>>>>>>>
>>>>>>>>>> 1       F       Yes            No          Yes            Unknown
>>>>>>>>>>
>>>>>>>>>> 3       M       No           Yes          Yes             No
>>>>>>>>>>
>>>>>>>>>> 5      F         Yes          Yes          Yes            Yes
>>>>>>>>>>
>>>>>>>>>> 9      M       Unknown    Yes          No             Yes
>>>>>>>>>>
>>>>>>>>>> 10     F        Yes           No           Yes             No
>>>>>>>>>>
>>>>>>>>>> I would like to produce a count of to show the number of people who
>>>>>>>>>> quit each year, only count the first time they quit and do not care if
>>>>>>>>>> they change their status to ‘No’ again.
>>>>>>>>>>
>>>>>>>>>> What would be the command? I tried to change it to the long formal and
>>>>>>>>>> use the lag function, but it doesn’t work.
>>>>>>>>>
>>>>>>>>> *
>>>>>>>>> *   For searches and help try:
>>>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>>>>
>>>>>>>> *
>>>>>>>> *   For searches and help try:
>>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>>>
>>>>>>> *
>>>>>>> *   For searches and help try:
>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>>
>>>>>> *
>>>>>> *   For searches and help try:
>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>
>>>>> *
>>>>> *   For searches and help try:
>>>>> *   http://www.stata.com/help.cgi?search
>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index