Announcement

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

  • Generating data for one variable based on two or more variables that have data.

    Hello,
    I'm kindly requesting for guidance on this.
    I am trying to create a variable age whose information is based on four variables.
    In this case, if an observation/id has a value for one variable, it should have data for the other 3.
    An example of the code is below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ptid age_m age_f age_j age_p)
    1 23  .  .  .
    2  . 44  .  .
    3  . 16  .  .
    4 45  .  .  .
    5  . 51  .  .
    6 65  .  .  .
    7  .  . 27  .
    8  .  .  . 36
    end
    I'm not sure if the explanation is helpful but I want to create a variable age with just one value based on the variables age_m age_f age_j and age_p

    I'll be grateful for any help.

    Thanks.

  • #2
    Patrick:
    do you mean something along the following lines?
    Code:
    . egen wanted=rowtotal(age_*)
    
    . list
    
         +-----------------------------------------------+
         | ptid   age_m   age_f   age_j   age_p   wanted |
         |-----------------------------------------------|
      1. |    1      23       .       .       .       23 |
      2. |    2       .      44       .       .       44 |
      3. |    3       .      16       .       .       16 |
      4. |    4      45       .       .       .       45 |
      5. |    5       .      51       .       .       51 |
         |-----------------------------------------------|
      6. |    6      65       .       .       .       65 |
      7. |    7       .       .      27       .       27 |
      8. |    8       .       .       .      36       36 |
         +-----------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      This is very helpful Carlo. Thanks a lot.

      Would you consider a similar approach if you were dealing with dates or strings?

      Thanks,
      Paddy

      Comment


      • #4
        Patrick:
        as far as I know, that -egen-function does not work with -string- variables.
        As far as dates are concerned I've neither used -rowtotal()- with dates, nor I can figure out an instance in which that approach is useful.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment

        Working...
        X