Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

Develop a program in Stata to handle multiple files in work folder just like SAS using tempfile

I am developing this wrapper around tempfile in Stata to handle multiple datasets at the same time without having to save them in current directory. So in essence I want Stata to mimic SAS, and that is the reason the program name is work; the initial code is without any options and minimal functionality. I have adhere to your advice not to tokenize and program works; however the issue is once I recall the tempfile in further program, this program (work) do not pass the handle to the next to call the tempfile.

code for "work"

capture program drop work
program define work
version 12.1
syntax [name(id="file name")] [, sort(string) ]
*tokenize namelist
*di "namelist'" 
*if "nol'" !="" | "nol'" !="" | "nolabel'" !="" { 
      * di "``namelist''" 
*} 
tempfile namelist' 
*di "namelist'" 
if "sort'" != "" { 
    sortsort' 
} 
di "File 'namelist'' saved in following temporary work folder" 
save ` namelist''

*di "Now you can use temporary file =namelist'' in your program" 
end

Once I call the temporary file within my do file as follows this error emerge:

work Orig File 'Orig' saved in following temporary work folder file C:Userssaa602AppDataLocalTempST_00000001.tmp saved

. di "`Orig'"

. use `Orig', replace

invalid file specification

Please note that I have been using tempfile successfully to handle multiple files, especially once merging large number of files without any hassle; however a wrapper program with options to sort and show the work directory location would be very handy for all of us.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As noted by @Roberto Ferrer, the program is difficult to read. (Some of us have tried to improve the presentation but not with the intent of fixing code errors.) Indeed it contains numerous fatal small errors, principally near randomness in the use of single quotation marks in the referencing of local macros

 `   ' 

Quotation marks that you need are missing; extra quotation marks that would be fatal in your case have been supplied; you use the wrong quotation marks in some lines. The claim that the program works is unsubstantiated therefore and appears absurd. At best, you have presented your program very carelessly and unprofessionally.

There is one central misunderstanding that undermines the intent of the program totally, however. A temporary file as named by tempfile and created somehow within a program will not survive beyond the end of a program. That is the precise sense in which a file is temporary. So, it is futile to imagine that using temporary files is a way to save files for any later purpose. Even if it were, there would be precisely no gain over using a file directly.

A marginal exception is that sometimes if a Stata session crashes, temporary files linger beyond the end of a program, but as that is essentially unpredictable, it has no bearing on your project.

A further extraordinary feature of your program is at best what it would do is save the current dataset in one temporary file. There is nothing in your code that has anything whatsoever to do with multiple files.

Wanting to mimic what SAS does is not a transparent aim to Stata programmers; my guess is that the fraction of Stata programmers familiar with SAS is of the order of 1%, so you would need to explain more unless you are aiming at a readership of that 1%. To make this into a question of any value, you would minimally need to explain what by handling several datasets at the same time actually would mean in Stata terms. Stata's idea is that datasets that you want to relate can be combined using append, merge, and other such commands, but that does not seem anything at all like what you want.

On advice to avoid tokenize: I don't think that is clear at all. What advice from whom, and more importantly why?

If you want to show us a program that works, and ask for advice on how to extend it, that could make sense, but at present it appears that your idea is impossible from the outset. Presenting code strewn with errors and claiming that it works is very puzzling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...