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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Reply: Setting default .do file template?


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Reply: Setting default .do file template?
Date   Wed, 28 Nov 2012 18:24:55 +0100

On Wed, Nov 28, 2012 at 11:48 AM, Maarten Buis <maartenlbuis@gmail.com> wrote:

> Maybe something like the program below. Store the part between
> *----------- begin program ----- and *------ end program ------ in a
> file called doedit2.ado, and store it somewhere where Stata can find
> it. A good place is returned by Stata if you type in Stata -personal-.
> After that the new command -doedit2- is available. If you type
> -doedit2- in Stata you will be asked to give the project name, analyst
> name and file name, and it will create the file with the header you
> requested and open it in the dofile editor.

<snip>

I had a bit of fun with doing some refinements, in particular add the
possibility that the specified file already exists, in which case it
adds the lines "Revised on: <date>" and "Revised by: <name>" at the
bottom of the header.

I am not sure where this little project is heading. I like the general
idea, but now it is too restrictive; maybe someone wants the header to
be in Dutch instead of English, or wants to add a line specifying the
Sub-project, or ... So that would require the creation by the user of
some sort of set-up file, that should be shareable across computers so
all members of the same team use the same set-up file, and the
interface to the creation of such a set-up file should be easy enough
for a general user. It may also make sense to let -doedit2- interface
with the user through a dialog box.

*----------------------------- begin program ---------------------------------
*! version 1.1.0 MLB 28Nov2012
program define doedit2
        display "Analyst: "   _request(_analyst)
        display "This file: " _request(_file)
        Parse_filename `file'
		local file `r(file)'
		
		tempname dofile
		
		capture confirm new file "`file'"
		if _rc {
			tempname dofile2
			tempfile foo
			file open `dofile' using "`file'", read
			file open `dofile2' using `foo', write
			file read `dofile' line
			file write `dofile2' `"`macval(line)'"' _n
				while r(eof) == 0 {
				file read `dofile' line
				if `"`line'"' == "*********** end header ***********" {
					file write `dofile2' "*" _n
					file write `dofile2' `"* Revised on: `c(current_date)'
`c(current_time)'"' _n
					file write `dofile2' `"* Revised by: `analyst'"' _n
				}
				file write `dofile2' `"`macval(line)'"' _n
			}
			file close `dofile'
			file close `dofile2'
			copy `file' `file'.bak, replace
			copy `foo' `file', replace
			doedit `file'
			exit
		}
		
        display "Project: "   _request(_project)

		file open dofile using `file', write
        file write dofile `"*********** begin header **********"' _n
        file write dofile `"* Project:    `project'"' _n
        file write dofile `"* Analyst:    `analyst'"' _n
        file write dofile `"* Date:       `c(current_date)'
`c(current_time)'"' _n
        file write dofile `"* This file:  `file'"' _n
        file write dofile `"*********** end header ***********"' _n
        file write dofile _n
        file write dofile "version 12" _n
        file write dofile "clear all" _n
        file write dofile "macro drop _all" _n
        file close dofile

        doedit `file'
end

program define Parse_filename, rclass
	local file `0'
	local file : list clean file
	local file : subinstr local file `"""' "", all
	mata reverselastpart("file","tocheck")
	gettoken od : tocheck, parse(".")
	if "`od'" != "od" {
		local file `file'.do
	}
	return local file "`file'"
end

mata
void reverselastpart(string scalar locname, string scalar newlocname) {
	string scalar name
	real scalar k
	
	name = st_local(locname)
	k = strlen(name)
	if (k < 200) {
		st_local(newlocname,strreverse(name))
	}
	else {
		k = k - 199
		name = substr(name, k,.)
		st_local(newlocname,strreverse(name))
	}
}
end
*------------------------------ end program ----------------------------------

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index