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

st: RE: how to keep only the first letter of a string type variable

From   "Ypma, Jelmer Yeb" <[email protected]>
To   <[email protected]>
Subject   st: RE: how to keep only the first letter of a string type variable
Date   Fri, 15 Oct 2004 15:55:22 -0700

Hello Sarah,
> I have a variable of string type and I only want to keep the first
> letter in each observation. I tried to use "abbrev" function as below:
> gen str11  newcountry = abbrev( country,1)
> but it didn't work. Could you help me please? 

Stata has a function -substr-

    substr(s,n1,n2) returns the substring of s starting at n1 for a length of n2.  If n1<0,
        the starting position is interpreted as distance from the end of the string.  If n2
        is missing (.), the remaining portion of the string is returned.

For instance if you have a variable "date" with values
substr(date,1,4) will return the first 4 digits
and substr(date,-2,.) will return the last two digits

Good luck!



This email message is for the sole use of the intended recipient(s) and
may contain privileged information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message.

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index