Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Saving Output from "describe" as its own dataset


From   "David Elliott" <[email protected]>
To   [email protected]
Subject   Re: st: Saving Output from "describe" as its own dataset
Date   Sun, 16 Sep 2007 14:21:41 -0300

This sounds very much like the COPY STRUCTURE EXTENDED commands that
database programs use which creates a dataset with complete
descriptions of the fieldnames and datatypes, indexes and other
metadata.  Such a dataset can then be used to specify a template for a
new, empty dataset.

Such functionality could be developed for Stata by looping through the
varlist and -postfile-ing the variable names and types, optionally
with formats, variable labels and value labels, to a dataset - about
15 minutes work.

x---------------------------------------x

*! version 1.0.0  2007.09.16
program define copystruct /* Copy filestructure to a dataset */
version 9.0

syntax using [, replace]

// Get variable string lengths
local varnamlen 0
local varlablen 0
local vallablen 0
local valfmtlen 0
foreach V of varlist _all {
	local varnamlen = max(length("`V'"),`varnamlen')
	local varlablen = max(length("`:var lab `V''"),`varlablen')
	local vallablen = max(length("`:val lab `V''"),`vallablen')
	local valfmtlen = max(length("`:format `V''"),`valfmtlen')
	}
tempname ph
postfile `ph' str`varnamlen' varnam str10 vartyp str`varlablen' varlab
 str`vallablen' vallab str`valfmtlen' varfmt `using' ,`replace'

foreach V of varlist _all {
	local varnam = "`V'"
	local vartyp = "`:type `V''"
	local varlab = "`:var lab `V''"
	local vallab = "`:val lab `V''"
	local valfmt = "`:format `V''"
	post `ph' ("`varnam'") ("`vartyp'") ("`varlab'") ("`vallab'") ("`valfmt'")
	}
	
postclose `ph'
end

x---------------------------------------x

DC Elliott
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index