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: -tabulate- looped over levels with -foreach-: how to count each subject only once, and display row percentages?


From   Michael McCulloch <[email protected]>
To   [email protected]
Subject   Re: st: -tabulate- looped over levels with -foreach-: how to count each subject only once, and display row percentages?
Date   Mon, 3 Feb 2014 10:31:53 -0800

Many thanks Sergiy, this works perfectly. Thanks for pointing out necessity of quotes around `l'.



Best wishes,
Michael McCulloch

--
Pine Street Foundation, since 1989
124 Pine Street | San Anselmo | California | 94960-2674  
P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org

On Feb 3, 2014, at 8:54 AM, Sergiy Radyakin wrote:

> Michael, tabulate wouldn't know if it is executed within loop or not
> and it doesn't care. Perhaps the error is in another part of the code?
> The following code demonstrates that looping works as expected.
> However if your Group variable is a string, then don't forget to put
> compound quotes around `l' and be careful with the length of the
> macro.
> Best, Sergiy
> 
> sysuse auto, clear
> rename foreign Gender
> rename mpg Age
> rename rep78 Group
> tabulate Gender Age, row
> qui levelsof Group, local(levels)
> foreach l of local levels {
>    tabulate Gender Age if Group == `l', row
> }
> 
> 
> On Sat, Feb 1, 2014 at 3:12 PM, Michael McCulloch
> <[email protected]> wrote:
>> I've written this command to produce a table, in which I seek to display variables <Age> and <Gender> in columns, over levels <Group> in rows:
>> 
>> qui levelsof Group, local(levels)
>> foreach l of local levels {
>>    tabulate Gender Age if Group == `l', row
>> }
>> 
>> Two questions about which I'd like to ask advice:
>> 1. In my dataset there are multiple observations per Subject. How can I count each Subject only once?
>> 2. Although one can use the -row- option with -tabulate-, in this case within -foreach- looping, the program returns "option row not allowed."
>> 
>> Any advice would be appreciated.
>> 
>> 
>> Best wishes,
>> Michael McCulloch
>> 
>> --
>> Pine Street Foundation, since 1989
>> 124 Pine Street | San Anselmo | California | 94960-2674
>> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org
>> 
>> 
>> *
>> *   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