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: store tabulate command results in excel


From   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: store tabulate command results in excel
Date   Tue, 13 Mar 2012 10:09:15 -0500

Hi Anisa:

You've introduced new vars (e.g., "feedback") that you haven't described before, so I'm not sure what your new code is trying to do.
-tabout- cannot "nest" in other identifiers on the rows in the exported table.  You can add it to the "condition" label with:

************
bys Condition: g row = string(_n)
replace Condition = Condition + " " + row
************

and now the row number would print in the condition label and you would get one row in the -tabout- table per Condition/row pair.  If you put some sort of delimiter (like pipe "|") between condition and row in your label, you could separate it in post-processing in Excel (text to columns feature) or Stata by making the "|" a column break.


If "Row" is just the observation number in your dataset for each individual answer/response, then why use -tabout-?  Just -export excel- or -outsheet- the data to a spreadsheet since you don't need to tabulate anything.

That is, if you take the table in your last message and make "Answer" the cross-variable (assuming "Row" is, as you depict, not duplicated ) using -tabout- you would get:


  +------------------------------------------------+
  | quest | cond | row | freq_YES | freq_NO |
  |----------+----------+-----+----------+---------|
  |        1 |        1 |   1 |        1 |       0 |
  |----------+----------+-----+----------+---------|
  |        1 |        1 |   2 |        1 |       0 |
  |----------+----------+-----+----------+---------|
  |        1 |        1 |   3 |        0 |       1 |
  |----------+----------+-----+----------+---------|
  |        1 |        1 |   4 |        0 |       1 |
  |----------+----------+-----+----------+---------|
  |        1 |        1 |   5 |        0 |       1 |
  |----------+----------+-----+----------+---------|
  |        1 |        2 |   1 |        1 |       0 |
  |----------+----------+-----+----------+---------|
  |        1 |        2 |   2 |        0 |       1 |
  |----------+----------+-----+----------+---------|
  |        1 |        2 |   3 |        0 |       1 |
  |----------+----------+-----+----------+---------|
  |        1 |        2 |   4 |        0 |       1 |
  +------------------------------------------------+

Which isn't a very helpful tabulation.

If none of the above are correct, please show a snippet of your actual data and what you want the -tabout- to show.  You keep changing the question and data description.


- Eric


__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Mar 13, 2012, at 8:57 AM, Anisa Shyti wrote:

> Hi Eric,
> 
> One more detail on this same issue addressed above.
> 
> If I want to refine the analysis even more, and add one more level of
> analysis as follows - Row - is the additional detail:
> 
> QuestionID.......Condition......Row.......Answer
> 1..........................1...............1............Y
> 1..........................1...............2............Y
> 1..........................1...............3............N
> 1..........................1...............4............N
> 1..........................1...............5............N
> 1..........................2...............1............Y
> 1..........................2...............2............N
> 1..........................2...............3............N
> 1..........................2...............4............N
> 
> I would like to have the precise detail of number and  % of Y answers
> given the QuestionID and Feedback.
> 
> I tried to modify the code as follows:
> 
> ******************************************************
> 
> cap rm "testQIDrow.xls"
> levelsof questionid, loc(q)
> foreach n in `q' {
> tabout feedback row choice ///
>    if questionid == `n'   ///
>  using "testQIDrow.xls", append  ///
>    c(freq row) h1(Question `n' )
>       }
> 	
> 
> 	
> levelsof feedback, loc(c)
> foreach v in `c' {
>       loc j `" `j' `v' "- feedback `v'" "'
>       }
> 
> lab def jj `j', modify
> lab val feedback jj
> ta feedback
> 
> levelsof row, loc(d)
> foreach r in `d' {
> loc i `" `i' `r' '
> }
> lab def ii `i', modify
> lab val row ii
> ta row
> 
> ******************************************************************
> 
> This code does return a tabulation for Row, but not per each category
> of feedback.
> 
> How shall the code be modify to suit Row nested into feedback?  Hope I
> am clear enough.
> 
> Thanks a lot.
> Anisa
> 
> 
> 
> 
> On Tue, Mar 13, 2012 at 1:16 AM, Anisa Shyti <[email protected]> wrote:
>> Thanks Eric!
>> 
>> Problem solved - works wonderfully!!!!
>> 
>> Thanks a lot,
>> Cheers
>> Anisa
>> 
>> On Mon, Mar 12, 2012 at 10:57 PM, Eric Booth <[email protected]> wrote:
>>> <>
>>> 
>>> BTW, if you wanted to add the labels to "Condition" in the -tabout- table as you do in your example, then add this code into your do-file just before you run the loop I gave you:
>>> 
>>> ************ add in to prev. code example
>>> levelsof Condition, loc(c)
>>> foreach v in `c' {
>>>        loc j `" `j' `v' "- Condition `v'" "'
>>>        }
>>> 
>>> lab def jj `j', modify
>>> lab val Condition jj
>>> ta Condition
>>> ************
>>> 
>>> - Eric
>>> 
>>> __
>>> Eric A. Booth
>>> Public Policy Research Institute
>>> Texas A&M University
>>> [email protected]
>>> +979.845.6754
>>> 
>>> On Mar 12, 2012, at 4:54 PM, Eric Booth wrote:
>>> 
>>>> 
>>>> 
>>>> <>
>>>> 
>>>> The answer is basically the same then (save a correction in my previous email noted below).
>>>> 
>>>> So, you could run:
>>>> 
>>>> ****************
>>>> clear
>>>> 
>>>> input QuestionID   Condition str2  Answer
>>>> 1     1         Y
>>>> 1     1         N
>>>> 1     1         N
>>>> 1     2         Y
>>>> 1     2         Y
>>>> 1     2         N
>>>> 2     1         Y
>>>> 2     1         Y
>>>> 2     1         N
>>>> 2     2         Y
>>>> 2     2         N
>>>> 2     2         N
>>>> end
>>>> 
>>>> 
>>>> ******
>>>> cap rm "test.xls"
>>>> levelsof QuestionID, loc(q)
>>>> foreach n in `q' {
>>>>  tabout Condition Answer ///
>>>>     if QuestionID == `n' /* I left this part out in the previous answ.*/  ///
>>>>   using "test.xls", append  ///
>>>>     c(freq row) h1(Question `n' )
>>>>        }
>>>> ****************
>>>> 
>>>> - Eric
>>>> 
>>>> __
>>>> Eric A. Booth
>>>> Public Policy Research Institute
>>>> Texas A&M University
>>>> [email protected]
>>>> +979.845.6754
>>>> 
>>>> On Mar 12, 2012, at 4:38 PM, Anisa Shyti wrote:
>>>> 
>>>>> Hi Eric,
>>>>> 
>>>>> Thanks for the answer.
>>>>> 
>>>>> Condition is actually a categorical variable coded as (1, 2, 3).
>>>>> Answer is string and there are 12 questions - questionIDs are
>>>>> integers.  My data structure is the following:
>>>>> 
>>>>> QuestionID..............Condition................Answer
>>>>> 1..............................1.............................Y
>>>>> 1..............................1.............................N
>>>>> 1..............................1.............................N
>>>>> 1..............................2.............................Y
>>>>> 1..............................2.............................Y
>>>>> 1..............................2.............................N
>>>>> 2..............................1.............................Y
>>>>> 2..............................1.............................Y
>>>>> 2..............................1.............................N
>>>>> 2..............................2.............................Y
>>>>> 2..............................2.............................N
>>>>> 2..............................2.............................N
>>>>> 
>>>>> 
>>>>> My table should look like:
>>>>> 
>>>>> .................................Answer...........................
>>>>> ................................Y..................N..........Total
>>>>> Question1................
>>>>> -Condition1..............nr(%).............nr(%).....nr(%)
>>>>> -Condition2..............nr(%).............nr(%).....nr(%)
>>>>> Question2
>>>>> -Condition1..............nr(%).............nr(%).....nr(%)
>>>>> -Condition2..............nr(%).............nr(%).....nr(%)
>>>>> 
>>>>> Thank you very much in advance.
>>>>> Anisa
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> On Mon, Mar 12, 2012 at 10:21 PM, Eric Booth <[email protected]> wrote:
>>>>>> <>
>>>>>> 
>>>>>> I don't understand your data structure (e.g. what are conditions?) and why -tabout- (from SSC, BTW) does not work.  Providing a snippet of your dataset and the command you tried would help me provide a better answer.
>>>>>> 
>>>>>> 
>>>>>> Based just on your sample -tab- command, you can transform that to -tabout- with something like:
>>>>>> 
>>>>>> 
>>>>>> ******
>>>>>> cap rm "test.xls"
>>>>>> levelsof question, local(ques_numbers)
>>>>>> foreach n in `ques_numbers' {
>>>>>>  tabout condition answer using "test.xls", append  ///
>>>>>>     c(freq row) h1(Question `n' )
>>>>>>        }
>>>>>> ******
>>>>>> ((The looping part is doing the -bysort question- part of your example -tab- command.))
>>>>>> 
>>>>>> - Eric
>>>>>> 
>>>>>> __
>>>>>> Eric A. Booth
>>>>>> Public Policy Research Institute
>>>>>> Texas A&M University
>>>>>> [email protected]
>>>>>> +979.845.6754
>>>>>> 
>>>>>> On Mar 12, 2012, at 4:07 PM, Anisa Shyti wrote:
>>>>>> 
>>>>>> 
>>>>>>> I need to store in excel or any other support the results of a two-way
>>>>>>> tabulate command.  My variables are: question, condition and asnwer.
>>>>>>> I have N questions, three conditions for each question, and Y or N
>>>>>>> answers by subjects.
>>>>>>> 
>>>>>>> The tab command (by question, sort: tab condition answer, row) gives
>>>>>>> exactly what I need. For each question (distinguished by id) and each
>>>>>>> condition, I have the number and percentage of Y and N answers and the
>>>>>>> Total - in columns.
>>>>>>> 
>>>>>>> Still, I would like to be able to store these results in excel or any
>>>>>>> other support, instead of copy pasting.  I tried using the tabout
>>>>>>> command (my case is a 2x1 table) - it does not give me the desired
>>>>>>> format.
>>>>>>> *
>>>>>>> *   For searches and help try:
>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>> *   http://www.stata.com/support/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/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/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/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/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index