Yes, it would. My mistake. [And as a perpetual novice, I was so excited
about finally believing I was able to answer a question on Statalist,
not just ask them :) ]
But how about this slight modification. Perhaps not as elegant as other
solutions posted:
sysuse auto, clear
gen N=_n
collapse (count) N, by (foreign)
list
+---------------+
| foreign N |
|---------------|
1. | Domestic 52 |
2. | Foreign 22 |
+---------------+
save newdatafile.dta
Another example:
sysuse auto, clear
gen N=_n
collapse (count) N, by(mpg)
+---------+
| mpg N |
|---------|
1. | 12 2 |
2. | 14 6 |
3. | 15 2 |
4. | 16 4 |
5. | 17 4 |
|---------|
6. | 18 9 |
7. | 19 8 |
8. | 20 3 |
9. | 21 5 |
10. | 22 5 |
|---------|
11. | 23 3 |
12. | 24 4 |
13. | 25 5 |
14. | 26 3 |
15. | 28 3 |
|---------|
16. | 29 1 |
17. | 30 2 |
18. | 31 1 |
19. | 34 1 |
20. | 35 2 |
|---------|
21. | 41 1 |
--Chris
--
Christopher W. Ryan, MD
SUNY Upstate Medical University Clinical Campus at Binghamton
and Wilson Family Practice Residency, Johnson City, NY
cryanatbinghamtondotedu
GnuPG and PGP public keys available at http://pgp.mit.edu
"If you want to build a ship, don't drum up the men to gather wood,
divide the work and give orders. Instead, teach them to yearn for the
vast and endless sea." [Antoine de St. Exupery]
Ronnie Babigumira wrote:
> But wouldn't this give one value which is just the total count of x not
> the counts for the different levels?
>
> [email protected] wrote:
>> I think this will work:
>>
>> -collapse (count) X-
>>
>> Don't type the hyphens. That is Statalist convention for indicating
>> Stata
>> code.
>>
>> Then save your data *with a different file name*. Simply saving it will
>> replace your original data (although Stata will warn you about this.)
>>
>> --Chris Ryan
>>
>>
>>
>>> Hi, everyone,
>>>
>>> As a new user, could any one show me how to save the results as a data
>>> file so that I can reuse it.
>>>
>>> Say, I have a variable (x) with many levels in a file. I want to get
>>> the total count of each level in a file.
>>>
>>> The codes;
>>>
>>> Sort x;
>>> By x: count;
>>>
>>>
>>> Will produce the results on the screen, but I want it in a file like:
>>>
>>>
>>> X N
>>> 1 5
>>> 2 10
>>> .....
>>>
>>>
>>>
>>> Thank you
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Shihe Fan
>>> Epidemiologist
>>> Office of the MOH
>>> Public Health Division
>>> Suite 300, 10216 - 124 Street
>>> Edmonton AB T5N 4A3
>>> Phone:(780) 413-7742
>>> Fax: (780) 413-7950
>>> [email protected]
>>>
>>> *
>>> * For searches and help try:
>>> * http://www.stata.com/support/faqs/res/findit.html
>>> * http://www.stata.com/support/statalist/faq
>>> * http://www.ats.ucla.edu/stat/stata/
>>>
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/support/faqs/res/findit.html
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>>
> *
> * For searches and help try:
> * http://www.stata.com/support/faqs/res/findit.html
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/