Bookmark and Share

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


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

st: RE: Problems with execution of conditional branching..


From   "Sarah Edgington" <[email protected]>
To   <[email protected]>
Subject   st: RE: Problems with execution of conditional branching..
Date   Wed, 13 Mar 2013 18:22:26 -0700

I think Scott's suggestion about how to tackle this problem probably offers
a more efficient strategy than this one.
However, know why this example doesn't work will probably also be helpful to
you in the future.

The problem is that the if statements here are only evaluating your first
observation.  See -help ifcmd- and -help if- for more details.
The thing that I think isn't entirely clear from the helpfile is that the
programming if checks the conditional statement once and if it's true it
takes the action in the braces.  Unless otherwise specified it evaluates the
first observation in the data when the conditional involves a variable.
To get an observation by observation evaluation of a conditional you have to
use the if expression after a command.
So -gen event_month=substr(opendate,6,1) if comma_3==2 & comma_2==1- will
check that condition for each observation and create the relevant variable.

Contrast that with a simplification of your first conditional (written as a
single line instead of in braces)
-if comma_3==2 & comma_2==1 gen event_month = substr(opendate, 6,1)-
Here Stata is only evaluating the conditional once.  Since it involves
variables (which, by virtue of being variables are going to be different
across observations) there has to be some rule about what observation is
used to evaluate this statement.  Since you haven't specified an observation
number to evaluate, it defaults to the first observation.  

So running your code will perform only one of the four possible sets of
commands you specified.  Which one it performs depends entirely on the first
observation in the data.

Hopefully that helps clear up what's going on here.

-Sarah





-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Mani Subramani
Sent: Wednesday, March 13, 2013 3:39 PM
To: [email protected]
Subject: st: Problems with execution of conditional branching..

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/

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