-2

Is there a way to do the following Stata code in R?

I want to create a staggered model, and I want to use the values of certain variables for 1994 for people who turned 40 in 1998; and want to use the values of the same variables for 1996 for people who turn 40 in 2000.

for any out temp emp edu married inc age \ var cesd1998 bitemp96 employ94 edu93 married94 inc94 age94 : gen XM2=Y if H0000200==1998

for any out temp emp edu married inc age \ var cesd2000 bitemp98 employ96 edu95 married96 inc96 age96 : replace XM2=Y if H0000200==2000
Aslı Gürer
  • 39
  • 1
  • 9
  • 4
    There are very few people who can look at Stata code and then give you an equivalent R program. (I once tried and found the Stata help pages totally opaque.) You will need to produce a minimal example coded in R and describe in natural language what is needed without phrases like "certain variables". Build a small example. Name the variables. And if this is the same question as was not answered 2 days ago, probably because of lack of these features, then copy the good parts into this one and delete the other one. – IRTFM Mar 18 '16 at 08:36
  • 2
    @42- +1. Be assured that Stata makes sense if you study and work with it. I presume the same of R. – Nick Cox Mar 18 '16 at 09:43
  • It is very similar to the question that I asked few days ago but I thought maybe it would make more sense in this way. I guess it does not – Aslı Gürer Mar 18 '16 at 15:26
  • @NickCox: I'm sure that Stata makes sense and is reasonably efficient. My reason for recommending R to my employer over the options of JMP which they offered me or Stata, which did have an excellent reputation and at the time was reasonably priced, was my prior experience with GLIM that transferred nicely over to R and the fact that R's Mac implementation at that time 2008 was capable of handling much larger datasets than Windows. If Stata had been freely available for personal "experimentation", I might have "played" with it during the run-up to that employment engagement. – IRTFM Mar 18 '16 at 16:59

1 Answers1

1

This isn't an answer, but won't fit easily into a comment. I don't even attempt R code. It seems to me that fluent R coders, not me, can reasonably expect, as an absolute minimum, some clarity on how you are holding the data in R.

The Stata syntax here is far from current, but was obsolete as of Stata 7. for in the sense here is not even documented any more.

This doesn't qualify as a minimal, complete, verifiable example: https://stackoverflow.com/help/mcve

for any out temp emp edu married inc age \ var cesd1998 bitemp96 employ94 edu93 married94 inc94 age94 : gen XM2=Y if H0000200==1998

for any out temp emp edu married inc age \ var cesd2000 bitemp98 employ96 edu95 married96 inc96 age96 : replace XM2=Y if H0000200==2000

One translation into current Stata is

 local x1list "out temp emp edu married inc age" 
 local x2list "out temp emp edu married inc age" 
 local y1list "cesd1998 bitemp96 employ94 edu93 married94 inc94 age94" 
 local y2list "cesd2000 bitemp98 employ96 edu95 married96 inc96 age96" 
 local nvars : word count `x1list' 

 forval j = 1/`nvars' { 
     local x : word `j' of `x1list' 
     local y : word `j' of `y1list' 
     replace `x'M2 = `y' if H0000200==1998
     local x : word `j' of `x2list' 
     local y : word `j' of `y2list'
     replace `x'M2 = `y' if H0000200==2000    
 } 

Not at all central, but note in passing that one reason the code is so awkward is that your naming conventions for variables are not consistent.

Community
  • 1
  • 1
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Thank you very much for the current Stata code. I forgot to delete one from the first list, the difference between counts was caused by that. I just edited. Thank you for making me realize that – Aslı Gürer Mar 18 '16 at 15:23
  • I fixed my reply following your fix. I am not going to supply the R code you need as I am a Stata person. Unfortunately my present guess is that your question is too unattractive to R experts, as you don't explain how your data are held in R or attempt any R code. – Nick Cox Mar 18 '16 at 15:49
  • R provides a `read.dta` function that could be used to construct an R dataframe which could them be converted to a minimal example using the `dput` function. Looking at these two versions of Stata code I'm wondering if the answer is just to use `merge`? – IRTFM Mar 18 '16 at 16:50