Hi STATA users,

I have a data set and code as attached.

I want to create new columns with a prefix nmedi for the list of variables using a condition. But the code does not work. Could you help me to identify the problem? Thank you.



----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double maindemographicid long(visitdate2 visitdate3 visitdate4 visitdate5 visitdate6 visitdate7 visitdate11 visitdate12 visitdate13 visitdate14) float(medi medi_start_date medi_end_date)
12 19403 19415 19436 19492 19583 19795     . 20159 . 20549 0 . .
15 19424 19430 19450 19520 19613 19795 19963 20255 .     . 0 . .
16 19424 19430 19458 19520 19613 19786     . 20150 . 20521 0 . .
17 19459 19466 19494 19543 19627 19823     .     . .     . 0 . .
18 19508 19513 19536 19599 19697 19872     .     . .     . 0 . .
end
format %d visitdate2
format %d visitdate3
format %d visitdate4
format %d visitdate5
format %d visitdate6
format %d visitdate7
format %d visitdate11
format %d visitdate12
format %d visitdate13
format %d visitdate14
format %td medi_start_date
format %td medi_end_date


foreach each var of varlist visitdate2 - visitdate14 {

gen nmedi_`var' = 1 if inlist(medi, 1,2,3,4,9) & medi_start_date < `var'  & `var' < medi_end_date

replace nmedi_`var' = 2 if inlist(medi, 5, 6, 7, 8) & medi_start_date < `var' & `var' < medi_end_date

}
------------------ copy up to and including the previous line ------------------