Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index