Hello,
I have a dataset where one variable is a locality area. The variable is called ICS22CD, these localities are coded as QKK QKM QKQ etc. There are some 40 or so localities, each with their own code. For each locality there is a cost amount: I need to multiply the appropriate population for each ICS22CD code by the cost amount.
I wrote a loop to do this, I stored all the ICS22CD codes of interest in a local variable, and cost amounts in a different local variable.
The idea is that, in the below loop, where the ICS22CD code is QKK, the relevant population number would be multiplied by 29789.1377931, and where the ICS22CD code is QKM it would be multiplied by 5986.25689 etc There are 40 cost amounts - though some are set to 0, corresponding to the 40 localities. I am aware that the calculations below are doing more than multiplying - but you can see below the steps I need to carry out. I know the steps in the actual calculations are right because I have done the calculations manually in Stata, but would prefer to do this by a loop.
However, I get the error message 'QKK not found'. when I run the loop - QKK is one of the codes for ICS22CD, so I think I must have mis-specified something in the coding as I am still getting used to writing loops.
Can anyone advise on what the issue might
local icd QKK QKM
local cost 29789.1377931 5986.25689
foreach a of local icd {
foreach c of local cost
{
sum(RegPats) if ICS22CD == `a'
di r(sum)
local pop_sum = r(sum)
local mult = `c'/`pop_sum'
replace PFI2 = RegPats * `mult' if ICS22CD == `a'
}
}
I have a dataset where one variable is a locality area. The variable is called ICS22CD, these localities are coded as QKK QKM QKQ etc. There are some 40 or so localities, each with their own code. For each locality there is a cost amount: I need to multiply the appropriate population for each ICS22CD code by the cost amount.
I wrote a loop to do this, I stored all the ICS22CD codes of interest in a local variable, and cost amounts in a different local variable.
The idea is that, in the below loop, where the ICS22CD code is QKK, the relevant population number would be multiplied by 29789.1377931, and where the ICS22CD code is QKM it would be multiplied by 5986.25689 etc There are 40 cost amounts - though some are set to 0, corresponding to the 40 localities. I am aware that the calculations below are doing more than multiplying - but you can see below the steps I need to carry out. I know the steps in the actual calculations are right because I have done the calculations manually in Stata, but would prefer to do this by a loop.
However, I get the error message 'QKK not found'. when I run the loop - QKK is one of the codes for ICS22CD, so I think I must have mis-specified something in the coding as I am still getting used to writing loops.
Can anyone advise on what the issue might
local icd QKK QKM
local cost 29789.1377931 5986.25689
foreach a of local icd {
foreach c of local cost
{
sum(RegPats) if ICS22CD == `a'
di r(sum)
local pop_sum = r(sum)
local mult = `c'/`pop_sum'
replace PFI2 = RegPats * `mult' if ICS22CD == `a'
}
}

Comment