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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: Problems with execution of conditional branching..


From   Mani Subramani <subra010@umn.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Problems with execution of conditional branching..
Date   Wed, 13 Mar 2013 17:38:52 -0500

I am new to Stata but I have programmed before (SPSS, SAS) etc. andI
am confused by the behavior of STATA in executing the code below.

I am trying to parse a date-string that is currently in one variable
with format "yyyy-mm-dd-hh-ss"..it unfortunately has 1 digit months
and days. Further, because of the unconventional numbering of months
(0-11, rather than 1-12), I am unable to use the much easier
dofc(Opendate)) command build into STATA that does some intelligent
validation and fails to import dates like October 31 (which is
indicated in my data as 2012,9,31

I use the location of the comma separators to parse the string with
strpos to create variables...the code runs..but it clearly does not
work properly.
I still have some dates with ",7" which I find inexplicable because
the branch where this should get executed picks  only 1 character
dates.
I have four mutually exclusive branches to handle the different
combinations of 2 digit and 1 digit month, day...but for some
reason..this is not working properly.
Some of the statements within branches that are excluded by the if
seem to be getting executed...

This would be ideally executed with a Case statement -or with Regex (I
tried but couldnt figure it out)
 but the code below should be doing that but it clearly isn't. Thanks
in advance for your help
thanks in advance for your help
Mani
====================
gen event_year = substr(opendate, 1,4)  //this works just fine, year
always 4 digits
// I test for pattern of month, day based on location of comma3 and
comma2 in date string
//date string format:  yyyy,mm,dd - month, day can be either 1 or 2 digits
//  location of third comma - it is either in 9, 10 or 11

gen comma_3 = strpos(substr(opendate,9,3), ",") // 1= 1 dig month&
date, 3+2 dig month, date, 2 = unclear
gen comma_2 = strpos(substr(opendate,7,2), ",") //when comma3 is ;
comma_2==1 indicates m,dd; comma_2==2 - mm,d

if comma_3==2 & comma_2==1 {
gen event_month = substr(opendate, 6,1)
gen event_day = substr(opendate, 8,2)
}
else if comma_3==2 & comma_2==2 {
gen event_month = substr(opendate, 6,2)
gen event_day = substr(opendate, 9,1)
}

else if comma_3==1 {
gen event_month = substr(opendate, 6,1)
gen event_day = substr(opendate, 8,1)
}
else if comma_3==3 {
gen event_month = substr(opendate, 6,2)
gen event_day = substr(opendate, 9,2)
}

==================  results===
opendate	comma_3	comma_2	event_year	event_month	event_day
2012,9,18,13,21,12	2	1	2012	9	18
2012,9,18,10,51,33	2	1	2012	9	18
2012,9,22,16,23,48	2	1	2012	9	22
2012,10,29,14,59,52	3	2	2012	1	,2
2012,10,21,23,57,29	3	2	2012	1	,2
2012,10,22,20,10,23	3	2	2012	1	,2
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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