hi, I ve been trying to setup "drug dose " for each day of follow up for a pharmacoepi analysis using forv command but have been unsuccessful . here is an example, we ll just use follow up for 1 year (365 days) to make things easier


here is how my data is currently arranged:

table1
id dispense_day days_supply dose
1 1 5 45
1 340 10 50
2 20 5 30
2 30 5 50
3 50 10 60
4 100 10 10
n

i d like to rearrange the data so that the above data table is reflected for every day in the year . for example, for patient id 1, the drug is dispensed on day 1 for 5 days with a dose of 45mg per day. ideally, i d like to get it to look like this table:

table2
id dailydose1 dailydose2 dailydose3 dailydose4 dailydose5 dailydose6 dailydose... dailydose364 dailydose365
1 45 45 45 45 45 0 0 0 0
this would be done for every observation from table 1

i ve been trying variations of the forv command as follows with no luck.

forv 1=1/365{
gen dailydose`i'=0
replace dailydose`i'=dose if dispense_day<=`i' & (dispense_day+ days_supply -1)>=`i'
}

i keep getting errors using this kind of logic. i know the answer is in plain sight, but any help would be much appreciated.

thanks
vishal