Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: AW: combination foreach forvalues


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: AW: combination foreach forvalues
Date   Tue, 20 Oct 2009 12:12:30 +0200

<> 

The answer depends on whether the strings are unique (easy!) or not. Let`s
assume the latter:


*************
clear*

inp str20 mystring 
"hello"        
"good bye"     
"good morning" 
"yesterday"    
"today"        
"tomorrow"     
"today"
"yesterday"
end

compress
replace mystring=trim(mystring)

sort mystring
egen tag=tag(mystring)
gen x2=sum(tag)

list, noobs
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von John Bunge
Gesendet: Dienstag, 20. Oktober 2009 12:08
An: statalist@hsphsun2.harvard.edu
Betreff: st: combination foreach forvalues

Dear all

I have a string variable x1 with a list of values. I want to create a
numerical variable x2 in which the numbers correspond to the string values
in x1 in an ordered fashion (as a counter).

To illustrate, lets assume x1 contains all letters of the alphabet, and I
want x2 to contain a counter that corresponds to the position of the letter
in the alphabet, i.e. x1=a > x2=1, x1=b > x2=2, x1=c > x2=3, etc...

This seems to me like a combination of foreach and forvalues, but I cannot
find information on whether and how such thing is implementable in Stata.

Hope I have made myself clear.

Thanks for your help

John.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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