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

# Re: st: forvalues

 From Henrik Støvring To "statalist@hsphsun2.harvard.edu" Subject Re: st: forvalues Date Tue, 23 Oct 2012 12:52:32 +0000

```Glad it worked, but actually I think, I was spot on: There is no value
of 1 in your "single" variable, as documented by -levelsof-. You could
account for this by using:

forvalues i = 2/53 {
...etc...

although you again would run into trouble with no observations having
single equal to 40, 41, 44, etc. In short levelsof is the right solution
here, but you gave the impression that the single variable had all
values from 1 to 53, which was indeed not the case.

Best,

Henrik

On 10/23/2012 02:45 PM, - wrote:
> Thank you,
> there was no problem in the dataset, the "single" variable is made up
> of integers between 1 and 53.
> Because I tried lots of different things, it seemed to have a problem
> with using " forvalues i = ....", without declaring anything first,
> and similarly when I was trying to give the value of i to the "single"
> variable in the "if(single=='i')" expression.
> Eventually I was able to get it to work by writing the following:
>
> . levelsof single, local(singles)
> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
> 28 29 30 31 32 33 34 35 36 38 39 42 43 49 50 51 53:  this was output
>
> . foreach single in `singles' {
>    2. tabstat agein2009 if(single==`single'), by(sexcat) stats(mean sd
> n min max median p10 p25 p75 p90)
>    3. }
>
> Then I got the output of the tables I need, iteratively, for each
> value of "single".
>
> Thank you for helping.
>
>
> On 23 October 2012 13:20, Henrik Støvring <STOVRING@biostat.au.dk> wrote:
>> ...which sounds like a problem of the dataset, not of the code. Try a
>> codebook of the variable -single- to see what values, it actually contains:
>>
>> codebook single
>>
>> -Henrik
>>
>> On 10/23/2012 02:14 PM, - wrote:
>>> It still won't do it, it says "no observations", but there are
>>> observations there...
>>>
>>> On 23 October 2012 13:09, Henrik Støvring <STOVRING@biostat.au.dk> wrote:
>>>> Your accents are off. You need to replace 'i' with `i' - note the
>>>> difference in the first accent.
>>>>
>>>> Best,
>>>>
>>>> Henrik
>>>>
>>>> On 10/23/2012 02:04 PM, - wrote:
>>>>> hello all,
>>>>> I'm trying to build a simple loop, and have looked at Help for
>>>>> "foreach" and "forvalues", but can't get it to work. I just want to
>>>>> set a variable i to take consecutive values from 1 to 53, so use
>>>>> forvalues i=1/53, then give its value to a variable in my data called
>>>>> "single" (refers to the number of samples for each person) which has
>>>>> integer values ranging from 1 to 53, and create tables of statistics
>>>>> for age in men and women. So there would be 53 tables of ages, one for
>>>>> each number of samples, summarized by sex.
>>>>> Haven't use loops in stata before, I write:
>>>>>
>>>>> forvalues i = 1/53 {
>>>>>      2. tabstat age if(single=='i'), by(sexcat) stats(mean sd n min max
>>>>> median p10 p25 p75 p90)
>>>>>      3. }
>>>>>
>>>>> but the message is that 'i' is an invalid name. If I use
>>>>> if(single==i), it says "i not found". Can anyone tell me what I'm
>>>>> doing wrong?
>>>>>
>>>>> Thank you all
>>>>> *
>>>>> *   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/
>>>>>
>>>> --
>>>>
>>>> *Henrik Støvring, PhD*
>>>> Associate professor
>>>> stovring@biostat.au.dk
>>>> Phone +45 8716 7991
>>>> Fax +45 8716 7305
>>>> Web: au.dk/en/stovring@biostat <http://au.dk/en/stovring@biostat>
>>>>
>>>>
>>>>
>>>> Department of Public Health
>>>> Biostatistics
>>>> University of Aarhus
>>>> Bartholins Allé 2, Bldg 1261, 217
>>>> DK-8000 Aarhus C
>>>> Denmark
>>>>
>>>>
>>>> Department of Public Health, Aarhus University
>>>>
>>>> *
>>>> *   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/
>>>
>>>
>> --
>>
>> *Henrik Støvring, PhD*
>> Associate professor
>> stovring@biostat.au.dk
>> Phone +45 8716 7991
>> Fax +45 8716 7305
>> Web: au.dk/en/stovring@biostat <http://au.dk/en/stovring@biostat>
>>
>>
>>
>> Department of Public Health
>> Biostatistics
>> University of Aarhus
>> Bartholins Allé 2, Bldg 1261, 217
>> DK-8000 Aarhus C
>> Denmark
>>
>>
>> Department of Public Health, Aarhus University
>>
>> *
>> *   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/
>
>

--

*Henrik Støvring, PhD*
Associate professor
stovring@biostat.au.dk
Phone +45 8716 7991
Fax +45 8716 7305
Web: au.dk/en/stovring@biostat <http://au.dk/en/stovring@biostat>

Department of Public Health
Biostatistics
University of Aarhus
Bartholins Allé 2, Bldg 1261, 217
DK-8000 Aarhus C
Denmark

Department of Public Health, Aarhus University

*
*   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/
```