Statalist The Stata Listserver


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

Re: st: string management questions


From   n j cox <[email protected]>
To   n j cox <[email protected]>
Subject   Re: st: string management questions
Date   Thu, 10 May 2007 12:53:42 +0100

Typo removed


 Here is the more general example code in one for copiers and pasters.
 To apply to a different example:

 1. Change -myvar- to whatever is appropriate.
 2. Check that variable names -distinct- and -same- are not in use.
 3. Change the separator from , if needed.

-----------------------------------------------------------
split myvar, p(,)
replace myvar1 = trim(myvar1)
gen distinct = myvar1
gen byte same = 0

local nvars : word count `r(varlist)'
qui forval i = 2/`nvars' {
     replace myvar`i' = trim(myvar`i')
     replace same = 0
     local prev = `i' - 1
     forval j = 1/`prev' {
         replace same = 1 if myvar`i' == myvar`j'
     }
     replace distinct = distinct + "," + myvar`i' ///
         if !same & myvar`i' != ""
}
-----------------------------------------------------------

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