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

RE:st: string function


From   "Sidney Atwood" <satwood@hsphmail.harvard.edu>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE:st: string function
Date   Fri, 11 Oct 2002 08:05:09 -0400

/*  a snippet that will count the number of 
    occurences of characters in strings

    including the generation of lower case letters 
    for demonstration
    SSA 10/11/02

*/ 


/* we're just going to look for the first five 
   letters 
*/ 

#delimit ;

set obs 100;

gen str10 avar = "";

for num 1/10:replace avar = avar + char(int(uniform() * 6 + 97)) if X >
0 ;

/* so now we have strings of the first 5 letters
   the line below counts them

   note if the strings are exactly the same length
   it is unnecessary to test length
*/
/* for clarity output vars are initialized in a
   separate line
*/
for LETTER in any a b c d e:gen int LETTERcount = 0 ;


for LETTER in any a b c d e: 
  for COLUMN in num 1/10:
    replace LETTERcount = LETTERcount + 1 
      if substr(avar,COLUMN,1) == "LETTER" & 
         COLUMN <= length(avar) 
;

/* all done, look at the results */

for var *count:tab X;

/* This kind of thing can be reversed as well if you
   need to concatenate variables
*/

/***********************************
Sidney Atwood
Manager
Instructional Computing Facility
Harvard School of Public Health
Kresge, LL-17
677 Huntington Avenue
Boston, MA  02115
617-432-3134
617-432-4998 - Fax
************************************/

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