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

st: RE: how to list variables vertically


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: how to list variables vertically
Date   Mon, 8 Mar 2004 17:43:24 -0000

On a second take, I have renamed my previous 
offering -- as to me the problem described is
listing variables horizontally -- and hacked
at some limitations and a bug. -vertlist- is 
now -hlist-. 

. hlist mpg if foreign

  +---------------------------------------------------------------------------------+
  | obs: | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 |
  |  mpg | 17 | 23 | 25 | 23 | 35 | 24 | 21 | 21 | 25 | 28 | 30 | 14 | 26 | 35 | 18 |
  |---------------------+-----------------------------------------------------------|
  |    68    |    69    |    70     |    71     |    72     |    73     |    74     |
  |    31    |    18    |    23     |    41     |    25     |    25     |    17     |
  +---------------------------------------------------------------------------------+

. hlist mpg if foreign, noheader

  +---------------------------------------------------------------------------------+
  |  mpg | 17 | 23 | 25 | 23 | 35 | 24 | 21 | 21 | 25 | 28 | 30 | 14 | 26 | 35 | 18 |
  |---------------------+-----------------------------------------------------------|
  |    31    |    18    |    23     |    41     |    25     |    25     |    17     |
  +---------------------------------------------------------------------------------+

. hlist mpg turn trunk in 1/10 

  +---------------------------------------------------------+
  |  obs:    1    2    3    4    5    6    7    8    9   10 |
  |---------------------------------------------------------|
  |   mpg   22   17   22   20   15   18   26   20   16   19 |
  | trunk   11   11   12   16   20   21   10   16   17   13 |
  |  turn   40   40   35   40   43   43   34   42   43   42 |
  +---------------------------------------------------------+

*! NJC 1.0.0 8 March 2004
program hlist
	version 8 
	syntax varlist(numeric) [if] [in] [, * ]
	
	preserve 
	qui gen _varname = "" 
	
	qui if "`if'`in'" != "" { 
		tempvar id 
		gen long `id' = _n 
		levels `id' `if' `in' 
		local obs "`r(levels)'" 
		keep `if' `in' 
	} 	
	else { 
		numlist "1/`=_N'"
		local obs "`r(numlist)'" 
	} 	

	keep `varlist' 
	xpose, clear varname
	
	tokenize `obs' 
	local i = 0 
	foreach v of var v* { 
		char `v'[varname] "``++i''"
	}	
	char _varname[varname] "obs:" 

	list _varname v* , noobs subvarname `options' 
end 	


Nick 
[email protected] 

> -----Original Message-----
> From: Nick Cox 
> Sent: 08 March 2004 14:44
> To: '[email protected]'
> Subject: RE: how to list variables vertically
> 
> 
> You mean like this? 
> 
> . vertlist mpg
> 
>   
> +-------------------------------------------------------------
> ------------------------+
>   | variable |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 
> 10 | 11 | 12 | 13 | 14 | 15 |
>   |      mpg | 22 | 17 | 22 | 20 | 15 | 18 | 26 | 20 | 16 | 
> 19 | 14 | 14 | 21 | 29 | 16 |
>   
> |-------------------------------------------------------------
> ------------------------|
>   | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 
> | 28 | 29 | 30 | 31 | 32  |
>   | 22 | 22 | 24 | 19 | 30 | 18 | 16 | 17 | 28 | 21 | 12 | 12 
> | 14 | 22 | 14 | 15 | 18  |
>   
> |----+----+----+----+----+----+----+----+----+----+----+----+-
> ---+----+----+----+-----|
>   | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 
> | 45 | 46 | 47 | 48 | 49  |
>   | 14 | 20 | 21 | 19 | 19 | 18 | 19 | 24 | 16 | 28 | 34 | 25 
> | 26 | 18 | 18 | 18 | 19  |
>   
> |----+----+----+----+----+----+----+----+----+----+----+----+-
> ---+----+----+----+-----|
>   | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 
> | 62 | 63 | 64 | 65 | 66  |
>   | 19 | 19 | 24 | 17 | 23 | 25 | 23 | 35 | 24 | 21 | 21 | 25 
> | 28 | 30 | 14 | 26 | 35  |
>   
> |---------+---------+-----------------------------------------
> -------------+----------|
>   |   67    |   68    |    69    |    70    |    71    |    
> 72    |    73    |    74    |
>   |   18    |   31    |    18    |    23    |    41    |    
> 25    |    25    |    17    |
>   
> +-------------------------------------------------------------
> ------------------------+
> 
> . vertlist mpg in 1/10
> 
>   +------------------------------------------------------------+
>   | variable    1    2    3    4    5    6    7    8    9   10 |
>   |------------------------------------------------------------|
>   |      mpg   22   17   22   20   15   18   26   20   16   19 |
>   +------------------------------------------------------------+
> 
> . vertlist mpg weight turn trunk in 1/10
> 
>   
> +-------------------------------------------------------------
> -------------------+
>   | variable      1      2      3      4      5      6      7 
>      8      9     10 |
>   
> |-------------------------------------------------------------
> -------------------|
>   |      mpg     22     17     22     20     15     18     26 
>     20     16     19 |
>   |    trunk     11     11     12     16     20     21     10 
>     16     17     13 |
>   |   weight   2930   3350   2640   3250   4080   3670   2230 
>   3280   3880   3400 |
>   |     turn     40     40     35     40     43     43     34 
>     42     43     42 |
>   
> +-------------------------------------------------------------
> -------------------+
> 
> *! NJC Statalist hack 8 March 2004
> program vertlist
> 	version 8 
> 	syntax varlist [if] [in] [, * ]
> 	
> 	preserve 
> 	qui gen _varname = "" 
> 	keep `varlist' 
> 	if "`if'" != "" { 
> 		keep `if' 
> 	} 	
> 	
> 	local l = _N 
> 	xpose, clear varname
> 
> 	foreach v of var v* { 
> 		local j = substr("`v'",2,.) 
> 		char `v'[varname] "`j'"
> 	}	
> 	char _varname[varname] "variable" 
> 
> 	if "`in'" != "" { 
> 		local in : subinstr local in "in " "" 
> 		local in : subinstr local in "l" "`l'" 
> 		local in : subinstr local in "f" "1" 
> 		numlist "`in'" 
> 		foreach j in `r(numlist)' { 
> 			local vlist "`vlist'v`j' " 
> 		}
> 	}	
> 	else local vlist "v*" 
> 				
> 	list _varname `vlist' , noobs subvarname `options' 
> end 	
> 
> Nick 
> [email protected] 
> 
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]]On Behalf Of 
> LACHENMAIER
> > Sent: 08 March 2004 14:13
> > To: [email protected]
> > Subject: st: how to list variables vertically
> > 
> > 
> > Hi all,
> > 
> > this might be a very simple question, but I have not found 
> a solution
> > yet.
> > 
> > Is it possible to create a list or table with the variables 
> appearing
> > vertically and the observations horizontally?
> > 
> > The output should look like 
> > 
> >       Observ1 Observ2 Observ3
> > Var1   xx1     xx2     xx3      
> > Var2   yy1     yy2     yy3                  
> > Var3   zz1     zz2     zz3
> > 
> > 
> > This type of presenting the data would be very useful to compare two
> > observations. Of course one can do it by copying the data 
> to Excel and
> > transpose the data, but this gets very inconvenient if the number of
> > variables is larger than the number of columns in Excel (one has to
> > paste it first horizontally before one can transpose it).
> > 
> > Thanks a lot,
> > Stefan
> > 
> > 
> > *
> > *   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/
> > 
> 

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