Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Variable generation

    Hi all how I can create caseid2 from caseid please

    caseid is case identification


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float caseid2 str15 caseid
    9172 "        3  2  2"
    9173 "        3 48  1"
    9174 "        3 52  2"
    9175 "        3 52  3"
    9176 "        3 71  3"
    9177 "        3108  2"
    9178 "        3127  2"
    9179 "        3154  1"
    9180 "        3168  2"
    9181 "        3168  3"
    9182 "        3168 18"
    9183 "        3246  2"
    9184 "        3246  3"
    9185 "        3257  1"
    9186 "        3279  2"
    9187 "        3279  3"
    9188 "        3279  4"
    9189 "        3279  5"
    9190 "        3309  2"
    9191 "        3309  3"
    9192 "        3335  2"
    9193 "        3335  3"
    9194 "        3345  2"
    9195 "        3367  2"
    9196 "        3376  2"
    9197 "        3376  3"
    9198 "        3376  4"
    9199 "        3414  2"
    9200 "        3451  2"
    9201 "        3451  4"
    9202 "        3451  7"
    9203 "        4 91  1"
    9204 "        4119  2"
    9205 "        4136  4"
    9206 "        4136  5"
    9207 "        4188  2"
    9208 "        4191  2"
    9209 "        4216  2"
    9210 "        4253  2"
    9211 "        4334  2"
    9212 "        4334  3"
    9213 "        4436  1"
    9214 "        4436  4"
    9215 "        4452  2"
    9216 "        4455  2"
    9217 "        4456  1"
    9218 "        4456  2"
    9219 "        4456  3"
    9220 "        8 44  2"
    9221 "        8 44  3"
    9222 "        8 44  5"
    9223 "        8 44  6"
    9224 "        8 83  1"
    9225 "        8165  2"
    9226 "        8165  3"
    9227 "        8177  1"
    9228 "        8198  2"
    9229 "        8199  2"
    9230 "        8199  3"
    9231 "        8243  2"
    9232 "        8248  3"
    9233 "        8248  4"
    9234 "        8261  2"
    9235 "        8265  2"
    9236 "        8265  3"
    9237 "        8271  2"
    9238 "        8308  2"
    9239 "        8311  7"
    9240 "        8311  8"
    9241 "        8311  9"
    9242 "        9  5  2"
    9243 "        9 46  1"
    9244 "        9 52  1"
    9245 "        9 52  2"
    9246 "        9 73  2"
    9247 "        9135  2"
    9248 "        9191  1"
    9249 "        9206  2"
    9250 "        9209  1"
    9251 "        9240  3"
    9252 "        9267  2"
    9253 "        9307  3"
    9254 "        9307  6"
    9255 "        9309  1"
    9256 "        9309  5"
    9257 "        9352  1"
    9258 "        9352  3"
    9259 "        9379  4"
    9260 "        9405  2"
    9261 "        9415  2"
    9262 "        9415  4"
    9263 "        9418  1"
    9264 "        9424  2"
    9265 "        9424  3"
    9266 "        9424  4"
    9267 "        9424  5"
    9268 "       14 22  2"
    9269 "       14 51  2"
    9270 "       14147  2"
    9271 "       14194  2"
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 8899 observations
    Use the count() option to list more


  • #2
    It is not evident why you want to start the sequence for caseid2 at 9172, but whatever the reason may be, it can be done with:
    Code:
    egen `c(obs_t)' wanted = group(caseid)
    replace wanted = 9171 + wanted
    Note: This code was written to still work in the event that in the full data set there may be multiple observations per caseid, or that the caseid variable may not always be in "numerical" order as it is in the example. But if, in fact, the full data has only one observation per caseid, then you can do it even more simply:
    Code:
    sort caseid
    gen `c(obs_t)' = 9171 + _n

    Comment


    • #3
      Thanks

      getting the following error I have more than one obs per caseid please

      . gen `c(obs_t)' = wanted +_n
      too few variables specified
      r(102);


      Comment


      • #4
        gen `c(obs_t)' = wanted +_n
        does not appear in any of the code I posted in #2.

        There is, in fact an error in the second block of code, which should have read
        Code:
        sort caseid
        gen `c(obs_t)' wanted = 9171 + _n
        But, as I indicated in #2, this block of code cannot be used with your data if you have more than one obs per caseid. So please use the first block of code in #2, which can accommodate multiple observations per caseid.

        Comment

        Working...
        X