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

Re: st: list long string variables in Stata8.2 SE


From   Arnold Kester <[email protected]>
To   [email protected]
Subject   Re: st: list long string variables in Stata8.2 SE
Date   Sat, 28 Feb 2004 21:53:36 +0100

Arnold Kester wrote:
Dear listers,

I have several string variables of up to 244 chars length (stata-se). I often want to list these for a selection of the subjects and print a list of the patient identification nr (patnr) and the string var if that's not empty.

format stringvar %-244s
list patnr stringvar if stringvar~="", noobs notrim

does the trick. The format statement is to get left-aligned output, which already improves it a lot. An annoying point is that all string will be filled with blanks to the length of the longest string, wasting a lot of screen area/paper, and hampering readability.

My log file illustrates:
(this gets messed-up even more in the mail, sorry for that)
-------------------------------------------------------------------------------
> ----
log: /home/arnold/longstringdata.log
log type: text
opened on: 9 Feb 2004, 15:17:53

. insheet using longstringdata, delim(" ") clear
(2 vars, 3 obs)

. format stringvar %-244s

. list, notrim noobs

patnr stringvar
>
>
1 This is a long string, that is, it is fairly long, but not extremel
> y so
>
2 Ths s a shorty
>
>
3 This is really really really a very very very very very very long l
> ong
> long long long long long Long LOng LONG string

. log close
log: /home/arnold/longstringdata.log
log type: text
closed on: 9 Feb 2004, 15:18:23
-------------------------------------------------------------------------------
> ----

What I would like is this format for the list:

--------------------------------------------------
. list, notrim noobs really_nice_print

> patnr stringvar
1 This is a long string, that is, it is fairly long, but not
extremely so
2 Ths s a shorty
3 This is really really really a very very very very very very
long long long long long long long Long LOng LONG string
.
-----------------------------------------------------

Is this at all possible in Stata? Or should try and do it in latex?
Alas, no answers, so I wrote something myself. It's rather crude I'm afraid, but maybe useful for someone else, too. I appended it here.

Regards,
Arnold Kester

====================== listlong.do ================


capture program drop listlong

program define listlong
version 8.2

** Arnold Kester, 28 february 2004
** List a long string variable along with an id variable and
** possibly other short string vars and/or numerical variables,
** possibly with labels.

** usage: listlong idvar longstringvar extravar1 extravar2 ...

syntax varlist(min=2) [if] [in] [,Length(integer 60)]

** assume first var is patid, second is string, rest is extra

* display "`varlist'", "`if'"

local patnr=word("`varlist'",1)
local stringvar=word("`varlist'",2)

* extravars can be a list

local nvars=wordcount("`varlist'")

local extravars=word("`varlist'",3)

forvalues i = 4/`nvars' {
local extravars="`extravars'"+" "+word("`varlist'",`i')
}



* save data

preserve

qui {
tempvar omit

gen `omit'=1
replace `omit'=0 `if'
drop if `omit'==1

keep `patnr' `stringvar' `extravars'
replace `stringvar'=trim(`stringvar')
drop if `stringvar'==""


local i=1

tempvar longer lastspace line patstr

gen `longer'=1
gen `lastspace'=`length'+1

su `longer'
local more=r(mean)
while `more'>0 {

* cut the first length, assign remainder to orig var

gen `stringvar'`i'=substr(`stringvar',1,`length')
replace `stringvar'=substr(`stringvar',`length'+1,.)
replace `longer'=(`stringvar'~="")

* move the cut to the last space

replace `lastspace'=`length'+1
replace `lastspace'=`length'+1-index(reverse(`stringvar'`i')," ") if `longer'==1
replace `stringvar'=substr(`stringvar'`i',`lastspace'+1,.)+`stringvar'
* replace `stringvar'=" "+`stringvar' if `stringvar'~=""
replace `stringvar'`i'=substr(`stringvar'`i',1,`lastspace'-1)
su `longer'
local more=r(mean)
local i=`i'+1

}

drop `stringvar'
reshape long `stringvar', i(`patnr') j(`line')

format `stringvar' %-80s
drop if `stringvar'==""


foreach var in `patnr' `extravars' {
local vartype :type `var'
* disp "`vartype'"
if index("`vartype'","str")>0 {
replace `var'="" if `line'~=1
}
else {
local label :val label `var'
if "`label'"=="" {
local label="`var'"
label value `var' `label'
}
* disp "`label'"
label define `label' -1 " ", add
capture replace `var'=-1 if `line' ~= 1
}
}

replace `stringvar'=" "+`stringvar' if `line'~=1
}
list `patnr' `extravars' `stringvar', notrim noobs


restore
end

================== end listlong.do ==================




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