Announcement

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

  • Creating a conditional sum of variables

    I have a database with 2 kinds of variables:

    DT1_OMS, PDT2_OMS...(1 to 5): instrumentation (you can have up to 5, and there are 5 different instrumentations)
    PDT1_DIA : nÂș of days you have used this instrumentation

    I would like to create a new variable in wich sum all PDT*_DIA, but only for an specific instrumentations.

    I know how to do creating some other variables, but I wonder if it is possible to do it directly:

    egen float d_su = rowtotal(PDT1_DIA if PDT1_OMS=="57.94", PDT2_DIA if PDT2_OMS=="57.94", ...)

    This program does not work, but it is possible to do something like that?

    Thank you in advance

  • #2
    Welcome to Statalist!

    I think what you want is to use the cond function where you have been trying to put if clauses.
    Code:
    egen float d_su = rowtotal(cond(PDT1_OMS=="57.94",PDT1_DIA,0), cond(PDT2_OMS=="57.94".PDT2_DIA,0),...)

    Comment


    • #3
      rowtotal() expects a varlist, so that shouldn't work. A further problem is testing for equality with a decimal value, here 57.94.

      This example is salutary:

      Code:
       
      . set obs 1
      obs was 0, now 1
      
      . gen foo = 57.94
      
      . count if foo == 57.94
          0
      
      . count if foo == float(57.94)
          1
      search precision to find out why. So I suggest

      Code:
      gen d_su = 0 
      
      forval j = 1/2 { 
             replace d_su = d_su + PDT`j'_DIA if PDT`j'_OMS == float(57.94) 
      }
      for "any value of 2".

      Comment


      • #4
        Nick Cox is of course correct about the rowtotal() function. I was too focused on promoting the cond() function to solve the conditional sum to realize the situation was inappropriate. On the other hand, I think there's a reasonable possibiity that the OMS variables are not "numbers" but rather "numeric identifiers" like the ICD9 codes that turn up on Statalist occasionally. So I am inclined to accept the implication that they are stored as string variables to be compared, as they were, to string values. But that's a guess. It's clear that the statement of the problem misses a fair amount of the information necessary to fully understand the problem.

        Jose Valencia will perhaps be able to sort out what he needs from our responses. If not, Jose, please follow up here, first having reviewed the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, looking especially at sections 9-12 on how to best pose your question. It would be particularly helpful to post a small hand-made example, perhaps with fewer variables as well as fewer observations, showing the data before the process and how you expect it to look after the process. Or show us what you did, creating additional variables, that worked, again including some sample data.

        Nick's answer and mine show how difficult it is to fully understand a problem statment in the absence of attention to the guidance in the FAQ.

        Comment


        • #5
          As William rightly points out if the variables concerned really are string, then using double quotes is quite correct.

          Code:
          gen d_su = 0  
          forval j = 1/2 {        
               replace d_su = d_su + PDT`j'_DIA if PDT`j'_OMS == "57.94"  
          }
          My own blindness here stems from rarely using such string codes, but that's not an excuse.

          Comment


          • #6
            Thank you all for your quick and genlte responses

            Certainly, PDT*_OMS are string variables (belong to a software database that I cannot modify); so, until your responses I did like that (it works, but it is hard, since it is not the only variable creation I have to do):

            gen d_su1= PDT1_DIA if PDT1_OMS=="57.94"
            gen d_su2= PDT2_DIA if PDT2_OMS=="57.94"
            gen d_su3= PDT3_DIA if PDT3_OMS=="57.94"
            gen d_su4= PDT4_DIA if PDT4_OMS=="57.94"
            gen d_su5= PDT5_DIA if PDT5_OMS=="57.94"
            egen float d_su = rowtotal(d_su1 d_su2 d_su3 d_su4 d_su5)

            I do not know well the forval command, but it seems very useful, I will try this other way

            Have a nice sunday evening! Jose

            Comment


            • #7
              Your code should work because rowtotal() ignores missings by default.

              Comment


              • #8
                You are right, Nick, the only problem is the huge quantity of code that is necessary (with my syntaxis)
                Thank you for your responses
                Last edited by Jose Valencia; 14 Feb 2016, 16:56. Reason: Ambiguous post (before)

                Comment


                • #9
                  What is the huge quantity of code? Nick's solution in #3 is just four lines, one of which is just a closing curly brace! How much shorter do you want it to be?

                  Comment


                  • #10
                    Clyde, I was talking about my code (not Nick's, obviously!)

                    As I said, I did not known 'forval' command before, so I have to seen how it works. I have just understanded and done it and... IT IS AMAZING!!

                    Nick, thank you, very, very much, you save me an enormous amount of time and open me another dimension of Stata
                    Last edited by Jose Valencia; 14 Feb 2016, 16:45.

                    Comment


                    • #11
                      Dear Statalist members,

                      I am currently trying to follow this post to create a conditional count in Stata 18. But I couldn't solve my issue.

                      For this, I need to create two new variables:
                      1. Number of central cultures with S aureus
                      2. Number of peripheral cultures with S aureus
                      The report variables Cultivo1M1 and Cultivo1M2 are associated with OrigenCultivo1.
                      Similarly, Cultivo2M1 and Cultivo2M2 are associated with OrigenCultivo2, and so on.

                      I need to calculate how many results/cultures of "STAPHYLOCOCCUS AUREUS" I have, categorized by the type/origen of culture (central -0- vs. peripheral -1-).

                      Could anyone guide me on how to implement this in Stata?





                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input str21 Cultivo1M1 str8 Cultivo1M2 byte OrigenCultivo1 str21 Cultivo2M1 str8 Cultivo2M2 byte OrigenCultivo2 str33 Cultivo3M1 str29 Cultivo3M2 byte OrigenCultivo3 str21 Cultivo4M1 byte(Cultivo4M2 OrigenCultivo4)
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 0 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 ""                      "." . ""                      ""                      . ""                      . .
                      "NEGATIVO"              "." 1 "NEGATIVO"              "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              ""                      1 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "NEGATIVO"              "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              ""                      1 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "KLEBSIELLA PNEUMONIAE" 0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              ""                      1 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 0 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 0 "NEGATIVO"              "." 1 "NEGATIVO"              ""                      1 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 "STAPHYLOCOCCUS AUREUS" . 0
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              ""                      0 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 "STAPHYLOCOCCUS AUREUS" . 0
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      1 ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 0 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" ""                      0 "STAPHYLOCOCCUS AUREUS" . 0
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "NEGATIVO"              "." 1 ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      ""                      "." . ""                      "." . ""                      ""                      . ""                      . .
                      "STAPHYLOCOCCUS AUREUS" "." 1 "STAPHYLOCOCCUS AUREUS" "." 1 ""                      ""                      . ""                      . .
                      end


                      Thank you in advance!

                      Best regards,
                      Last edited by Carolina Hincapie; 08 Jan 2025, 09:53.

                      Comment


                      • #12
                        The OrigenCultivo* variables, when not missing, are coded 0 and 1. I assume that one of these codes represents central and the other peripheral, but cannot guess which is which.

                        There is also a slight problem in that the Cultivo4M2 variable is numeric, whereas the other Cultivo*M* variables are strings.

                        To count the number of cultures growing S. aureus, by origin, it is best to first -reshape- the data into fully long layout:
                        Code:
                        gen `c(obs_t)' obs_no = _n
                        tostring Cultivo4M*, replace
                        reshape long Cultivo@M1 Cultivo@M2 OrigenCultivo, i(obs_no) j(_j)
                        reshape long CultivoM, i(obs_no _j) j(_k)
                        
                        by OrigenCultivo, sort: count if CultivoM == "STAPHYLOCOCCUS AUREUS"
                        Last edited by Clyde Schechter; 08 Jan 2025, 10:04.

                        Comment


                        • #13
                          Dear Clyde Schechter thanks for your answer, It does work perfect for a database counting, but not in a row (patient) counting.

                          What I need 2 new variables which there is the counter depending the type of culture




                          Sorry that I missed the labeI description before


                          Code:
                          label def OrigenCultivo 0 "central", modify
                          label def OrigenCultivo 1 "peripheral", modify

                          Comment


                          • #14
                            I think Clyde Schechter 's point -- which he usually spells out in detail -- is that wide layouts (some say formats, but layout is Clyde's word, and it's better in my view) are usually not so good for Stata purposes as long layouts. So a reshape long should not usually be reversed by a reshape wide -- although that is something you perhaps should have considered.

                            The essence of what I think you want otherwise is to initialise a counter at 0 and then loop over variables, bumping up the counter by 1 every time you see what you want.

                            Code:
                            ds , has(type string)
                            
                            gen wanted = 0
                            
                            foreach v of var `r(varlist)' {
                                replace wanted = wanted + (`v' == "STAPHYLOCCUS AUREUS")
                            }
                            That sketch needs to be elaborated to count two kinds separately -- so two counters and perhaps two separate loops -- and to be supplemented by a fix for whatever is numeric.

                            Much more at https://journals.sagepub.com/doi/pdf...867X0900900107 (for update, see https://journals.sagepub.com/doi/pdf...36867X20931007)

                            Note that such code is utterly literal, and may need elaboration for variations in spelling, spacing, use of lower or upper case, and so on.

                            Comment


                            • #15
                              Code:
                              gen `c(obs_t)' obs_no = _n
                              frame copy default original
                              tostring Cultivo4M2, replace
                              reshape long Cultivo@M1 Cultivo@M2 OrigenCultivo, i(obs_no) j(n1)
                              reshape long CultivoM, i(obs_no n1) j(n2)
                              gen byte is_s_aureus_ = CultivoM == "STAPHYLOCOCCUS AUREUS"
                              collapse (sum) is_s_aureus_, by(obs_no OrigenCultivo)
                              drop if missing(OrigenCultivo)
                              decode OrigenCultivo, gen(oc)
                              drop OrigenCultivo
                              reshape wide is_s_aureus_, i(obs_no) j(oc) string
                              recode is_s_aureus* (. = 0)
                              
                              frame change original
                              frlink 1:1 obs_no, frame(default)
                              frget _all, from(default)
                              Note: I suspect your full data set actually contains a patient ID variable that uniquely identifies observations. If that is the case, you do not need to create the obs_no variable, and you can just replace obs_no by the name of the patient ID variable throughout the code.

                              Added: Crossed with #14. That code is obviously simpler, but, Nick is correct in pointing out that in general the use of long layouts is better in Stata, which is why I use a more roundabout code. I should also point out that the code in #14 does not create separate counts for cultures of central and peripheral origin: it creates a total of all cultures that grew out S. aureus.
                              Last edited by Clyde Schechter; 09 Jan 2025, 10:10.

                              Comment

                              Working...
                              X