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]

st: save list of scalars or locals to file


From   <[email protected]>
To   <[email protected]>
Subject   st: save list of scalars or locals to file
Date   Mon, 16 Apr 2012 13:13:56 +0200

Dear Statalisters,


I'm wondering if there is any fast and easy way to export values stored in Stata memory (here: scalars or locals) to a textfile in specific order.

In my example each local or scalar stores one line of text. 
The order is relevant and specified by stub and number in scalar or local names
I use scalars (h1, h2, h3, ... h`sc'). 
Local 'sc' stores the maximum scalar number.

I basically have two workarounds both using the data sheet:

*****
1. Data sheet.
clear
set obs `sc'
gen str244 var1 = ""
qui forval x = 1/`sc' {
replace  var1 = h`x' if `x' == _n 
}

Fills scalars into the data sheet line after line. Can be exported via - outfile -.
Limitations: very slow with big `sc'; 244 character limit.

*****
2. Log file and data sheet
This way is due to Daniel Kleins very useful hint to display lists of scalars using macro extended functions: 
pr scadir
	vers 9.2
	scalar dir `: all scalars `"`0'"''
end

All displayed scalars are logged and imported to the data sheet:

set linesize 244
log using d:/temp/log.txt, replace text
scadir h*
log close 
infix str244 var1 1-244 using "D:\temp\log.txt", clear
...

After import into the data sheet further commands clean the data, extract scalar numbers to sort the lines and scalar text for export via - outfile -.
More steps but in the end much faster than looping like in (1).
Limitations: more error prone than (1), 230+ character limit (depends on scalar/local name length) 

*****
Any other suggestions?


Best wishes 

Stefan Gawrich
Dillenburg
Germany





*
*   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