Announcement

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

  • shortcut to repeat command changing the number

    Hi!

    I have a dataset on admissions (50.000+ observations). As patient information was entered for every ward they have visited during their admission, I reshaped data in wide format because I just want to know date of arrival and discharge and not the “in-betweens”.

    As some will have had 1 shift during admissions and others 10 I’m running this command to get the correct discharge date:

    .gen dateoutend = .
    .replace dateoutend= dateout1 if dateout1 !=.
    .replace dateoutend= dateout2 if dateout2 !=.
    .replace dateoutend= dateout3 if dateout3 !=.
    …. And so on .. (I only have 6 this time but sometimes 27)

    I thought this would work:
    .replace dateoutend = dateout1-6 if dateout1-6 != .
    but it returns an incorrect date

    Is there any shortcut to these commands so I don’t have to write “dateout1” “dateout2” ect. Obs. Stata has to run the command in the correct order from 1-x.

    Regards Lykke (ps - my first entry - great forum)

  • #2
    if they are numeric, then -forvalues- if they may have arbitrary names -foreach-.

    Comment


    • #3

      In
      . replace dateoutend = dateout1-6
      you subtract 6 from dateout1, and that is not what you want. egen's rwomax function finds the highest nonmissing value in a variable list, i.e., the latest date recorded. I think that is what you need:

      Code:
      egen dateoutend = rowmax(dateout1-dateout27)

      Comment


      • #4
        Why did you reshape in the first place. You can use collapse on the long form data to reduce to one observation per patient with the first non-missing date in and last non-missing date out

        Code:
        clear
        input patient datein dateout
        1 1 1
        1 2 3
        1 3 7
        2 4 7
        2 8 10
        end
        format %td date*
        list, sepby(patient)
        
        collapse (firstnm) datein (lastnm) dateout, by(patient)
        list

        Comment


        • #5
          One quick note on Robert Picard's elegant solution: it only works if the data are sorted in the correct order. To be safe, you should put the appropriate sort command before the -collapse- command.

          Comment


          • #6
            Good catch Clyde and come to think of it, I don't know why I didn't think of simply using (min) and (max) instead.

            Code:
            clear
            input patient datein dateout
            1 3 7
            1 1 1
            1 2 3
            2 8 10
            2 4 7
            end
            format %td date*
            list
            
            collapse (min) datein (max) dateout, by(patient)
            list
            Last edited by Robert Picard; 06 Sep 2014, 10:14.

            Comment

            Working...
            X