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   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   Re: st: list long string variables in Stata8.2 SE
Date   Sun, 29 Feb 2004 11:13:47 -0500

I couldn't resist a quick stab at making this shorter and easier (without
preserving the data, reshaping,etc.)  The main short-cut is to use the
extended macro function -piece-.  Here's some code to do the basic id
variable plus list a long string.  I haven't put in anything to display more
than 2 variables and haven't dealt with value labels (add 2 lines of code),
but here's the basic approach:

program define listlong2
version 8.2
syntax varlist(min=2) [if] [in] [,Indent(integer 10) Length(integer 60)]
gettoken patnr varlist: varlist
gettoken stringvar varlist: varlist
marksample touse, novarlist
markout `touse' `patnr' `stringvar', strok
qui count if `touse'
local start=_N-r(N)+1
local end=_N
local maxpieces=ceil(244/`length')
sort `touse' `patnr'
forval i=`start'(1)`end' {
local id=`patnr'[`i']
local idlen=length("`id'")
local indent=max(`indent',`idlen'+1)
local pad=`indent'-`idlen'
local string=`stringvar'[`i']
forval j=1(1)`maxpieces' {
 local part`j': piece `j' `length' of `"`string'"'
 if `j'==1 di "`id'" _skip(`pad') `"`part`j'"'
 else if `"`part`j''"'!="" di _skip(`indent') `"`part`j'"'
}
}
end

Michael Blasnik
[email protected]


----- Original Message ----- 
From: "Arnold Kester" <[email protected]>
To: <[email protected]>
Sent: Saturday, February 28, 2004 3:53 PM
Subject: Re: st: list long string variables in Stata8.2 SE


> 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