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]

Re: st: Creating long, filledin dataset from two, year variables


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Creating long, filledin dataset from two, year variables
Date   Mon, 7 Mar 2011 01:20:28 +0000

Your question and code are very confused. Focusing on your code

foreach v of varlist start_date* - end_date* {
       gen new`v' = `v' - `v' if substr("`v'",-3,.) != substr("`v'",1,3)
}

-foreach- will cycle over a set of variables -- one at a time, which
is not I guess what you want. But your minus sign in the varlist is
illegal, which is provoking the error message. A minus sign can be
part of a varlist as in make-foreign, but it can't be a connector for
two wildcards. If you want it to function like a mathematical minus
sign, that is just guessing wrongly at what might work. The syntax for
-foreach- is that after -varlist- only varlists are allowed.

For each variable, you try to generate a new variable, which is given
by the expression

`v' - `v'

but that reduces to zero if both variables are numeric. The first `v'
and the second `v' are one and the same to -foreach-.

In using the condition -if substr("`v'",-3,.) != substr("`v'",1,3)-
you appear to be confusing the -
if- command and the -if- qualifier. The condition translates as

if the last 3 characters of the variable name are not the same as the
first 3 character of the variable name

but that would always be true of your variables -start_date*- and
-end_date*- as you explain them, so that -if- shouldn't bite. Perhaps
you are here confusing the name of a variable and the values it
contains. Even then I can't guess at your intent.

Focusing on your question: It would help a lot if you just gave one
consolidated example with real, or realistic, data and not bits and
pieces of explanation and cross-references to another question. In
addition, are these numeric or string variables, etc.?

I read your posting twice and it is still unclear to me. Of course, if
someone else understands and gives you a solution, then that's fine by
me.

Nick (not Mr!)

On Sun, Mar 6, 2011 at 4:08 PM, Adrian Stork <[email protected]> wrote:

> I hope Kevin's question is answered because I'm dealing with a pretty similar
> issue but with an additional trick:
> There's not only one var for start-date and one var for end-date but a
> large number of start and end dates
> (startdate01-startdate80 & enddate01-endate80). Each start and end date indicate
> the time frame in which a doctor was active in project.
>
> So the varlist looks like:
> Project*    StartDate1       Endate1    Doctor1     StartDate2
> Enddate2    Doctor2   ...
>
> *A project can be led by different doctors across time and even
> simultaneously by several doctors.
>  A doctor can have several projects at the same time.
>
> For each doctor(i) I want to assign which projects he is/was working
> for on a monthly basis (Long Panel)
> The structure would be identical to Kevin's except for the additional projects:
> Doctor    month     Project1    Project2
> Smith     Jun2001    canc      vaccin
> Smith     Jul2001     canc       ...
> Smith     Aug2001      ...         ...
> Taylor     Jan1999      ...
>
> I tried to generate the date variable as in Kevin's case but "invalid
> name" came back:
>
> .foreach v of varlist start_date* - end_date* {
> .gen new`v' = `v' - `v' if substr("`v'",-3,.) != substr("`v'",1,3)
> .}
>
> I hope someone knows how to assign the monthly date and the projects
> to the doctors.
> I read the FAQs regarding -reshape-,but that didn't help either. I'd
> really appreciate any
> comment on that and thanks, Mr.Cox,for the answer to Kevin. It gave me
> a first perspective
> on the issue.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index