Announcement

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

  • Can you run forvalues with bysort to generate new variables?

    Dear Statalist,

    I have the following data. Each individual have done multiple angiographic examinations with each examinations possibly resulting in multiple treatments. Each observation is a treatment. Im trying to create a variable looping through each individual and identifying if a treated segment
    gets treated again in the next examination. I have tried to lift up the information using a loop but its inneffective and requires modification of the variables .

    Code:
    foreach a in 1 2 3 4 {
    by idnr : gen newvar`a' = segment_pci[_n+`a'] 
    by idnr : gen date_of_angio`a' = date_of_angio[_n+`a'] 
    }
    
    replace newvar1=. if newvar1!=segment_pci
    replace date_of_angio1=. if newvar1!=segment_pci

    My question is, is there a forvalues loop that can run through each individual, identifying if each treated segment appears in a different examination
    and create a new variable on the right segment as is shown in newvar and newvardate?

    idnr angio_nr date of angio segment_pci newvar newvardate
    54 1 18-Nov-11 8 1 28-Jan-12
    54 1 18-Nov-11 7 1 28-Jan-12
    54 1 18-Nov-11 6 1 28-Jan-12
    54 2 28-Jan-12 8
    70 1 26-Sep-10 2
    70 1 26-Sep-10 12
    70 1 26-Sep-10 6
    81 1 26-aug-10 6
    81 2 10-Apr-12
    81 2 10-Apr-12
    81 3 02-Aug-17
    81 4 23-Nov-17 2 1 23-Dec-17
    81 4 23-Nov-17 3 1 23-Dec-17
    81 4 23-Nov-17 1 1 23-Dec-17
    81 5 23-Dec-17 3
    86 1 09-Sep-13
    86 1 09-Sep-13 6
    86 2 09-Oct-13 2
    86 2 09-Oct-13 3
    86 2 09-Oct-13 1
    86 3 11-Dec-13 11 1 02-Jan-14
    86 4 02-Jan-14 11
    90 1 17-Jan-14
    90 2 25-May-18 11

    Sincerely,
    Moman
    and many thanks for a splendid forum!!!


  • #2
    As I understand your words, what they describe does not match what you show in your example. And I cannot discern from the code what you are trying to do. So I'm going to ignore your words and code and just try to replicate the example you show. My understanding of what you want is, therefore:

    If a segment that is treated on a given angiogram is again treated on any subsequent angiogram, mark all observations for that earlier angio with the date of the earliest retreatment, and create a variable that is 1 when for all observations of that earlier angio. You also appear to want to code this variable as 1 for yes and missing for no, but since that is bad coding practice and will likely get you into trouble when you try to work with the resulting variable, I will override that and code it as 1 = yes and 0 = no. The following code does all of this:

    Code:
    clear
    input byte(idnr angio_nr) float dateofangio byte segment_pci
    54 1 18949  8
    54 1 18949  7
    54 1 18949  6
    54 2 19020  8
    70 1 18531  2
    70 1 18531 12
    70 1 18531  6
    81 1 18500  6
    81 2 19093  .
    81 2 19093  .
    81 3 21033  .
    81 4 21146  2
    81 4 21146  3
    81 4 21146  1
    81 5 21176  3
    86 1 19610  .
    86 1 19610  6
    86 2 19640  2
    86 2 19640  3
    86 2 19640  1
    86 3 19703 11
    86 4 19725 11
    90 1 19740  .
    90 2 21329 11
    end
    format %td dateofangio
    
    
    by idnr segment_pci (dateofangio), sort: gen retreatment_date = dateofangio[_n+1]
    format retreatment_date %td
    replace retreatment_date = . if missing(segment_pci)
    by idnr angio_nr (retreatment_date), sort: replace retreatment_date = retreatment_date[1]
    gen byte retreatment = !missing(retreatment_date)
    Note: This code assumes that your dateofangio variable is a real Stata internal format numeric date variable. If you have it as a string variable that human eyes read as a date, you will have to convert it first, using the -daily()- function.

    Added: Also, I cannot tell what you would want to do if there were an angiogram for which 2 or more different treated segments were subsequently retreated, but on different dates. In the code I have given, the result would be the earliest of the retreatment dates.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
    Last edited by Clyde Schechter; 12 Oct 2019, 16:38.

    Comment


    • #3
      Dear Mr Schechter,

      Thank you for the thorough respone and I apologise for not being clear enough.
      Your example worked really well but I detected a small issue that I just cant figure out. Here is an example of patient 240 and his second coronary segment, which was retreated two times. The first time it is retreated works well and the date in the first row appears correct but not on the third row as the patient gets this segment retreated a second time. However, its a minor issue

      Code:
      clear
      input double idnr long interdat float segment_pci byte retreatment float retreatment_date
      240 19787 2 0 .
      240 19787 3 0 .
      240 19800 2 0 .
      240 21000 2 0 .
      end
      format %tdD_m_Y interdat
      format %td retreatment_date
      Again, thank you very much for the help!

      Comment


      • #4
        and here is another example where retreatment should be 1 in row 1 and 2 and I cant really figure out why it isnt. Any ideas?

        Code:
         input double idnr long interdat float segment_pci byte retreatment float retreatment_date
        1146    26 Apr 10    12    0    
        1146    26 Apr 10    12    0    
        1146    20 Feb 13    12    1    11oct2013
        1146    11 Oct 13    12    0    
        1146    11 Oct 13    12    0    
        1146    21 Jun 17    5    0    
        1146    21 Jun 17    11    0
        EDIT: Sorted it out by keeping 1 row for each segment! Thanks!
        Last edited by Moman Aladdin; 13 Oct 2019, 05:37.

        Comment

        Working...
        X