Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: forvalues foreach or while loop for sequential variables


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: forvalues foreach or while loop for sequential variables
Date   Thu, 31 Oct 2013 09:12:42 +0000

(Revised version of previous)

This picks up the syntax Robbie was missing, namely

`=exp'

for some expression exp, but it won't work entirely as desired.
Suppose -hospital7- -hospital8- -hospital9- are all 42. Then
-hospital8- is changed to missing because it is the same as
-hospital7-, but next time around the loop -hospital9- of 42 is
compared with missing and nothing is changed.

Roberto pointed out this problem too.

The following should be an improvement for a solution without -reshape-.

clear
 input obs hosp14  hosp15 hosp16
 1   1      5    5
 2   6      9    6
 3   8      1    3
 4   5      5    5
 5   2      2    2
 6   2      9    1
 7   1      1    1
 8   3      1    7
 9   3      0    4
 end

gen previous = hosp14
gen temp = .

qui forval j = 15/16 {
       replace temp = hosp`j'
       replace hosp`j' = . if hosp`j' == previous
       replace previous = temp
}

drop previous temp

That said,

1. The non-missing values left will not be guaranteed unique or
distinct. Hospitals 6, 9, 6 visited in that order will be left as is
and 6 will occur twice.

2. Throwing away information is always risky. Why do you want to do this?

3. As these are in essence panel data, Roberto's implication that in
many ways you are better off with a long data structure (I don't like
the word "format" here) is worth pondering.

> Nick
> njcoxstata@gmail.com
>
>
> On 31 October 2013 07:32, A Loumiotis <antonis.loumiotis@gmail.com> wrote:
>> another way without defining a new local would be:
>>
>> replace hosp`num'=. if hosp`num'==hosp`=`num'-1'
>>
>> Antonis
>>
>> On Thu, Oct 31, 2013 at 8:52 AM, Roberto Ferrer <refp16@gmail.com> wrote:
>>> Elaborating a bit more, according to your example, you intend
>>> something along the lines of:
>>>
>>> *-------- begin code ---------
>>> clear
>>> set more off
>>>
>>> input obs hosp14  hosp15 hosp16
>>> 1   1      5    5
>>> 2   6      9    6
>>> 3   8      1    3
>>> 4   5      5    5
>>> 5   2      2    2
>>> 6   2      9    1
>>> 7   1      1    1
>>> 8   3      1    7
>>> 9   3      0    4
>>> end
>>>
>>> list
>>>
>>> * You have this in a loop
>>> replace hosp15 = . if hosp15 == hosp14
>>> list
>>> *--------- end code ----------
>>>
>>> but this compares only contiguos hospitals (14 with 15 and 15 with 16) but not
>>> hospitals that are "further apart" (14 with 16). This is the case with
>>> observation 2 in the example data I created. Your loop misses this comparison,
>>> which I assume is important, i.e., you want hosp16 to equal .
>>> for that observation because hosp14 already takes the value of 6. If
>>> the assumption
>>> is true, this is very problematic.
>>>
>>> That said, now take a look at what your variables are evaluating to
>>> inside the loop
>>> (use -display- for this):
>>>
>>> *-------- begin code ---------
>>> clear
>>> set more off
>>>
>>> input obs hosp14  hosp15 hosp16
>>> 1   1      5    5
>>> 2   6      9    6
>>> 3   8      1    3
>>> 4   5      5    5
>>> 5   2      2    2
>>> 6   2      9    1
>>> 7   1      1    1
>>> 8   3      1    7
>>> 9   3      0    4
>>> end
>>>
>>> forvalues num=15/16 {
>>>           display "hosp`num'" // your LHS
>>>           display "hosp``num'-1'" // your RHS
>>>           replace hosp`num'=. if hosp`num'==hosp``num'-1'
>>> }
>>>
>>> *--------- end code ----------
>>>
>>> The LHS is OK, but the RHS is always evaluating to "hosp". You do not
>>> specify your
>>> error in your post (you mention "it didn't work" and you should be
>>> specific about it)
>>> but here I get this:
>>>
>>> hosp ambiguous abbreviation
>>> r(111);
>>>
>>> Stata doesn't know if you're refering to hosp14, hosp15 or hosp16, and
>>> so it halts
>>> with an error.
>>>
>>> Because I mess up easily with all the quotes (maybe someone will
>>> clarify this point),
>>> my strategy is to declare a new local and then use it for the RHS:
>>>
>>> *---
>>> forvalues num=15/16 {
>>>           local newnum = `num' - 1
>>>           replace hosp`num' = . if hosp`num' == hosp`newnum'
>>> }
>>> *---
>>>
>>> I hope this helps.
>>>
>>> On Thu, Oct 31, 2013 at 1:42 AM, Roberto Ferrer <refp16@gmail.com> wrote:
>>>> Robbie,
>>>>
>>>> Here is one way avoiding the loop and using -reshape-.
>>>>
>>>> *-------begin code ---------
>>>>
>>>> clear
>>>> set more off
>>>>
>>>> input obs hosp14  hosp15 hosp16
>>>> 1   1      5    5
>>>> 2   6      9    6
>>>> 3   8      1    3
>>>> 4   5      5    5
>>>> 5   2      2    2
>>>> 6   2      9    1
>>>> 7   1      1    1
>>>> 8   3      1    7
>>>> 9   3      0    4
>>>> end
>>>>
>>>> list
>>>>
>>>> * Reshape to manipulate
>>>> reshape long hosp, i(obs) j(hnum)
>>>>
>>>> sort obs hosp hnum // important
>>>> gen hosp2 = hosp
>>>> by obs: replace hosp2 = . if hosp[_n] == hosp[_n-1]
>>>>
>>>> drop hosp
>>>> rename hosp2 hosp
>>>>
>>>> * Take back to original form
>>>> reshape wide hosp, i(obs) j(hnum)
>>>> list // compare this with original input
>>>>
>>>> *------- end code ---------
>>>>
>>>> On Thu, Oct 31, 2013 at 12:41 AM, Robbie Katz <robbiekatz05@gmail.com> wrote:
>>>>> I have a wide format data with variables with same text and different
>>>>> numbers attached to them. The variables look like this: hospital1
>>>>> hospital2 hospital3...hospital15. I want only unique values across
>>>>> these variable for any one observation. For instance if both hospital
>>>>> 15 and hospital14 have the same value say, 10, then I want to delete
>>>>> 10 and replace it with "." for hospital15. Please let me know how I
>>>>> can run a loop to achieve this.
>>>>>
>>>>> I tried the following but it didn't work:
>>>>> forvalues num=2/15 {
>>>>>           replace hospital`num'=. if hospital`num'==hospital``num'-1'
>>>>>   }
>>>>>
>>>>> Please help,
>>>>> Thanks!
>>>>> *
>>>>> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index