Announcement

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

  • Replacing a value on a specific row

    Hi
    I want to replace the variable "age" with the inputs for another variable "agedobyear", but only for 4 specific values of another variable "id" that occurred on a specific "date" (another variable)

    I then want to round the new values of age (to the nearest whole number) for only those 4 specific id's on specific dates

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id int age float(agedobyear date)
    68995 0   .9637235 21378
    74197 0  .54757017 21302
    76033 0    .862423 21397
    83324 0   .5831622 21355
    84493 0   .4654346 21688
    85958 0   .3750856 21338
    86788 0          . 21275
    86903 0 .013689253 21277
    86916 0   .7008898 21277
    87258 0          . 21283
    87388 0   .7583846 21301
    87420 0          . 21284
    87564 0  .06570842 21286
    87697 0   .3367556 21288
    87703 0  .10951403 21335
    87889 0 .016427105 21291
    88227 0          . 21295
    88249 0          . 21295
    88279 0          . 21296
    88732 0   .2491444 21301
    88783 0          . 21302
    88890 0          . 21304
    88919 0   .2491444 21304
    88994 0   .6269678 21305
    89190 0   .1779603 21308
    89269 0   .6899384 21308
    89305 0          . 21309
    89506 0          . 21312
    89603 0  .05475701 21314
    89662 0  .07939767 21314
    89742 0          . 21316
    89760 0          . 21316
    89772 0  .03832991 21316
    89797 0 .002737851 21315
    89798 0          . 21315
    89856 0   .9144422 21318
    89894 0 .005475702 21318
    89939 0          . 21319
    89946 0          . 21319
    89957 0          . 21318
    90039 0   .8213552 21320
    90143 0          . 21321
    90581 0   .1779603 21327
    90591 0  .52019167 21327
    90626 0  .06570842 21327
    90829 0  .27104723 21330
    90863 0          . 21331
    90889 0   .4435318 21396
    90999 0          . 21333
    91021 0 .021902807 21333
    91056 0   .8651608 21334
    91155 0   .7118412 21334
    91159 0  .28473648 21334
    91203 0  .03011636 21335
    91318 0          . 21336
    91540 0   .3449692 21340
    91805 0   .6105407 21343
    91880 0          . 21345
    92055 0 .002737851 21347
    92342 0          . 21351
    92370 0          . 21351
    92375 0 .002737851 21351
    92388 0          0 21352
    92400 0 .005475702 21352
    92432 0  .09034908 21353
    92474 0   .8021903 21353
    92569 0          . 21354
    92626 0          . 21632
    92844 0          . 21357
    92845 0  .09582478 21357
    92891 0   .4982888 21358
    92984 0   .4325804 21359
    93049 0          . 21360
    93078 0 .002737851 21360
    93210 0          . 21361
    93211 0          . 21361
    93367 0  .13415469 21363
    93454 0          . 21364
    93512 0  .03285421 21365
    93645 0  .11498974 21368
    93840 0          . 21371
    93890 0  .03559206 21371
    93897 0  .14510609 21371
    94070 0          . 21375
    94113 0   .1998631 21375
    94199 0  .29021218 21376
    94245 0  .06023272 21377
    94475 0  .06023272 21380
    94498 0   .3477071 21381
    94533 0   .9034908 21381
    94537 0          . 21381
    94756 0   .7583846 21384
    94767 0   .3011636 21384
    94777 0 .002737851 21385
    94779 0   .4298426 21385
    94815 0 .002737851 21385
    94890 0          0 21387
    94930 0 .002737851 21388
    95015 0  .23545517 21388
    95077 0          . 21389
    end
    format %td date

    I want to do this for the following rows:
    id dob date age agedobyear
    149489 20-Dec-74 29-Jul-20 0 45.60712
    149489 20-Dec-74 12-Sep-21 46 46.72964
    157209 21-Apr-28 12-Nov-20 0 92.56126
    157209 21-Apr-28 20-Oct-21 93 93.4976
    159130 20-Jun-73 08-Dec-20 0 47.46886
    159130 20-Jun-73 06-Jun-21 47 47.96167
    164535 29-Apr-87 23-Feb-21 0 33.82341
    164535 29-Apr-87 07-Sep-21 34 34.36003
    I tried the following but it doesn't work

    Code:
    replace age = agedobyear if id == 149489 & date == "29-Jul-20"

    (sorry I have exceeded the time for editing the topic title so its not very explanative
    Last edited by Josie Thompson; 07 Jun 2022, 05:24.

  • #2
    The date variable is a SIF value, not a string. You may use the -td()- function, though.

    Code:
    replace age = agedobyear if id == 149489 & date ==td(29jul2020)
    See

    Code:
    help td()

    Comment


    • #3
      Originally posted by Josie Thompson View Post
      I then want to round the new values of age (to the nearest whole number) for only those 4 specific id's on specific dates
      You do not appear to be rounding up, which is natural for age. To chop off the fractional part, see the -int()- function.

      Code:
      help int()
      The suggestion in #2 now becomes

      Code:
      replace age = int(agedobyear) if id == 149489 & date ==td(29jul2020)

      Sorry, I missed this detail in my answer in #2.

      Comment

      Working...
      X