Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: Split string variable

From   daniel klein <>
Subject   Re: st: Split string variable
Date   Mon, 22 Oct 2012 14:24:29 +0200

Your -foreach- loop will not work for various reasons, but I will not
comment on that. You are looking for

g foo = substr(ccodealp_year, 1, 3)


Dear Statalisters,
I have the following problem that probably needs some intelligent
application of -substr- and/or -trim- , but that I have not been able
to achieve so far. I have a string variable (name "ccodealp_year")
that takes the following observations:

Each observation is a three-digit country code followed by two-digit
year. I need to create a variable consisting of just the three-digit
country codes, i.e. need to remove the two-digit years from each
observation of the string variable. I have also tried something like:
foreach w of varlist ccodealp_year { gen `ccodealp' =
"`ccodealp_year'-`year'" }, obviously without success. I was wondering
if anyone can point me to the right direction. Any help is greatly

Best regards.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index