Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Re: Writing values to a file


From   Janet Hill <janethill73@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: Writing values to a file
Date   Thu, 9 Dec 2010 09:41:39 +0000 (GMT)

Eric, David & Nick,
Many thanks for your help. Once again I have 
learned some new programming tips and my problem is solved.My problem was
 writing the data line to an output file - I will improve the quality of
 my question in future.

Janet


--- On Wed, 8/12/10, Eric Booth <ebooth@ppri.tamu.edu> wrote:

From: Eric Booth <ebooth@ppri.tamu.edu>
Subject: Re: st: Re: Writing values to a file
To: "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Date: Wednesday, 8 December, 2010, 15:41

<>

If understand your post correctly, you want the output in "elong" to have a header row that says: "repeat, e" and then rows following that which have the value of the variable "e" if the conditions you write are true as well as the "repeat" group number (`i').  So, it should look like:

*------elong.txt sample output-------*
(repeat, e)
(234, 1)
(345, 1)
(345, 2)
... and so on (?)


David has suggested that there are already several ways to save/export summary stats, or you could -keep- only the observations you're interested in and -outsheet- them, but if you'd prefer to use -file write- then these changes to your code should get you started (note that I changed r(min) to r(mean) so that there were values to work with in the auto.dta):

************!
sysuse auto, clear

**change vars in auto.dta to match**
rename rep78 repeat
recode repeat (. = 1)
rename price id1
rename mpg ld
replace ld = ld/100000
rename for group
rename turn e

**keep your vars**
keep ld id1 repeat group e



*****
capture : file close myfile
file open myfile using "elong.txt", write replace
    file write myfile "Results: (repeat,e)" _n

qui su repeat, d
forvalues i=1/`r(max)' {
tempvar marker
g `marker' = 0
qui replace `marker'=1 if repeat==`i' & ld > 0.001
**get e if repeat==i and id1==r(min) (or <r(mean))
qui levelsof e, loc(ef)
foreach v in `ef' {
tempvar ee eem
 qui su id1 if `marker'==0 & repeat==`i', d
g `ee' = e if repeat==`i' & id1<r(mean) & e ==`v'
egen `eem' = max(`ee')
di `eem'
    if `eem' != . {
    file write myfile "(`=`eem''" 
    file write myfile ","
    file write myfile "`i')"   _n
    }
    }
}
file close myfile
di as smcl `"Click to Open File:  {browse elong.txt}"'

************!

- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754



On Dec 8, 2010, at 9:23 AM, Airey, David C wrote:

> .
> 
> 
> So are you saying you can determine the summary statistics you want, but you cannot write them out to a text file? Why would you not save the summary statistics out into a Stata data set or .csv file from a Stata data set? In other words is it really the data management that is throwing you or the writing out of data to text files?
> 
>> I would be grateful for some advice on writing a value to a file. I have a dataset where I need select a particular value, the first value exceeding a threshold value,and the process is repeated over 240 groups. I can list the values but I am unable to write the values to a file for subsequent use. My code - and I know it is awful programming - is:
>> 
>> capture : file close myfile
>> file open myfile using "elong.txt", write replace
>>     file write myfile ("repeat,e") _n
>> capture : drop marker
>> gen marker=0
>> forvalues i=1/24{
>> replace marker=1 if repeat==`i' & ld > 0.001
>> su id1 if marker==0 & repeat==`i'
>> list group repeat l ls e if repeat==`i' & id1==r(min)
>>     file write myfile (e) if repeat==`i' & id1==r(min)
>>     file write myfile ","
>>     file write myfile (`i') _n
>> }
>> file close myfile
>> 
>> Many thanks.
>> Janet
> 
> 
> 
> 
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index