Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: -list-, -list-, -list-


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: -list-, -list-, -list-
Date   Thu, 27 Feb 2003 19:58:16 -0000

I had a play with a way of outflanking -list- in Stata 8, on 
behalf of those who don't like it. This is the result of 
a quick hack, and I make no claims to its great merits, 
but anyone who likes it is welcome to it. If you like minimal, 
this is minimal. (_No_ headers even.) 

-listout- has that name partly because of the way it borrows 
-outfile- to emit a file which it then types. 
Anyone who likes this may want to clone it as something 
with a shorter name. -lo- appears to be available. 

Here is the code: 

program listout 
*! NJC 1.0.0 26 Feb 2003 
	version 8 
	syntax [varlist] [if] [in] [, Comma NOLabel ljs rjs ]
	
	local njs : word count `ljs' `rjs' 
	if `njs' == 2 { 
		di "{p}{err}may not specify both " /// 
		"{inp:ljs} and {inp:rjs}{p_end}"
		exit 198 
	}
	else if `njs' == 1 { 
		local options "`rjs'"
	} 
	else if `njs' == 0 { 
		local options "fjs"
	}	

	if "`quote'" == "" { 
		local options "`options' noquote" 
	}	

	tempfile outfile
	tempvar obs 
	gen `obs' = "{txt: " + string(_n) + ".}{res}" 
	outfile `obs' `varlist' `if' `in' using `"`outfile'"', /// 
		wide `comma' `nolabel' `options'
	type `"`outfile'"', smcl 
end 	

The syntax is 

listout [varlist] [if] [in] [, Comma NOLabel ljs rjs ] 

where 

comma separates variables by commas (ugly but compressed) 

nolabel suppresses value labels 

ljs forces left-justification of string variables. 

rjs forces right-justification of string variables. 

Here are some examples, truncated to show style: 

. listout mpg
  1.        22  
  2.        17  
  3.        22  
  4.        20  
  5.        15  
  6.        18  
  7.        26  

. listout mpg if foreign
 53.        17  
 54.        23  
 55.        25  
 56.        23  
 57.        35  
 58.        24  
 59.        21  

. listout mpg foreign displa make
  1.        22  Domestic       121  AMC Concord         
  2.        17  Domestic       258  AMC Pacer           
  3.        22  Domestic       121  AMC Spirit          
  4.        20  Domestic       196  Buick Century       
  5.        15  Domestic       350  Buick Electra       
  6.        18  Domestic       231  Buick LeSabre       
  7.        26  Domestic       304  Buick Opel          

. listout mpg foreign displa make, nolabel
  1.        22         0       121  AMC Concord         
  2.        17         0       258  AMC Pacer           
  3.        22         0       121  AMC Spirit          
  4.        20         0       196  Buick Century       
  5.        15         0       350  Buick Electra       
  6.        18         0       231  Buick LeSabre       
  7.        26         0       304  Buick Opel          

. listout mpg foreign displa make, comma
 1.,22,Domestic,121,AMC Concord
 2.,17,Domestic,258,AMC Pacer
 3.,22,Domestic,121,AMC Spirit
 4.,20,Domestic,196,Buick Century
 5.,15,Domestic,350,Buick Electra
 6.,18,Domestic,231,Buick LeSabre   
 7.,26,Domestic,304,Buick Opel

Nick 
n.j.cox@durham.ac.uk 
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index