Announcement

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

  • Most Common Medication Unweighted and Weighted

    Hello,

    I'm currently working with NHANES data attempting to combine multiple survey years together to identify the most common prescriptions used over time and in specific cycles among a specific subpopulation. I am attempting to obtain weighted and unweighted results for the most common medications.

    The data, after combining together, is long. I converted it to wide; however, I have potentially drug1-drug 21 which I'm not quite sure how to identify most common from that. Instead, I'd think most prefer to work with the data in long form and collapse across cycles and by person. Any assistance would be appreciated either if the data is reported as long (shown below) or across with one row for each person in the cycle as drug1-drug21, for example.

    Code:
    *unweighted--overall and by subpop;
    tab rxddrug if cycle==1 & inAnalysis==1 & subpop!=., sort 
    tab rxddrug if cycle==1 & subpop==1 & inAnalysis==1, sort
    tab rxddrug if cycle==1 & subpop==0 & inAnalysis==1, sort
    I use 2 cycles here as an example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double seqn float cycle str114 rxddrug float(inAnalysis subpop)
    51624 1 "CIMETIDINE"                      0 0
    51624 1 "DULOXETINE"                      0 0
    51624 1 "LEVETIRACETAM"                   0 0
    51625 1 ""                                0 0
    51626 1 ""                                0 0
    51627 1 ""                                0 0
    51628 1 "CARVEDILOL"                      0 0
    51628 1 "ENALAPRIL; HYDROCHLOROTHIAZIDE"  0 0
    51628 1 "ESOMEPRAZOLE"                    0 0
    51628 1 "GLIMEPIRIDE"                     0 0
    51628 1 "LEVOTHYROXINE"                   0 0
    51628 1 "TRAMADOL"                        0 0
    51629 1 ""                                0 0
    51630 1 "ACYCLOVIR"                       0 0
    51630 1 "BUPROPION"                       0 0
    51630 1 "DOCUSATE"                        0 0
    51630 1 "HYDROCHLOROTHIAZIDE; OLMESARTAN" 0 0
    51630 1 "IBUPROFEN"                       0 0
    51631 1 "DONT KNOW"                       0 0
    51632 1 "METHYLPHENIDATE"                 0 0
    51633 1 "TAMSULOSIN"                      0 0
    51634 1 ""                                0 0
    51635 1 "AMLODIPINE"                      0 0
    51635 1 "METOPROLOL"                      0 0
    51635 1 "OMEPRAZOLE"                      0 0
    51635 1 "SIMVASTATIN"                     0 0
    51635 1 "TAMSULOSIN"                      0 0
    51636 1 ""                                0 0
    51637 1 ""                                1 0
    51638 1 ""                                0 0
    51639 1 ""                                0 0
    51640 1 "DEXMETHYLPHENIDATE"              0 0
    51640 1 "FLUTICASONE; SALMETEROL"         0 0
    51640 1 "LEVALBUTEROL"                    0 0
    51640 1 "MONTELUKAST"                     0 0
    51641 1 ""                                0 0
    51642 1 ""                                0 0
    51643 1 "BUPROPION"                       1 0
    51643 1 "CALCIUM ACETATE"                 1 0
    51643 1 "DICYCLOMINE"                     1 0
    51643 1 "EFAVIRENZ"                       1 0
    51643 1 "EMTRICITABINE"                   1 0
    51643 1 "INSULIN GLARGINE"                1 0
    51643 1 "LOSARTAN"                        1 0
    51643 1 "METOCLOPRAMIDE"                  1 0
    51643 1 "OMEPRAZOLE"                      1 0
    51643 1 "OXCARBAZEPINE"                   1 0
    51643 1 "PERPHENAZINE"                    1 0
    51643 1 "PRAVASTATIN"                     1 0
    51643 1 "PROPRANOLOL"                     1 0
    51643 1 "RALTEGRAVIR"                     1 0
    51643 1 "VALACYCLOVIR"                    1 0
    51644 1 "AZITHROMYCIN"                    0 0
    51645 1 "LOVASTATIN"                      0 0
    51645 1 "METOPROLOL"                      0 0
    51646 1 "BUDESONIDE"                      0 0
    51647 1 "BUPROPION"                       0 0
    51647 1 "FLUOXETINE"                      0 0
    51648 1 ""                                0 0
    51649 1 ""                                0 0
    51650 1 ""                                0 0
    51651 1 ""                                0 0
    51652 1 ""                                0 0
    51653 1 ""                                0 0
    51654 1 "HYDROCHLOROTHIAZIDE; LISINOPRIL" 0 0
    51655 1 ""                                0 0
    51656 1 "EZETIMIBE; SIMVASTATIN"          0 0
    51657 1 ""                                0 0
    51658 1 "ACETAMINOPHEN; HYDROCODONE"      1 0
    51659 1 ""                                0 0
    51660 1 ""                                0 0
    51661 2 "ALPRAZOLAM"                      0 0
    51661 2 "BROMAZEPAM"                      0 0
    51661 2 "CLINDAMYCIN"                     0 0
    51661 2 "DICYCLOMINE"                     0 0
    51661 2 "IBUPROFEN"                       0 0
    51661 2 "METOCLOPRAMIDE"                  0 0
    51661 2 "RABEPRAZOLE"                     0 0
    51661 2 "TRAMADOL"                        0 0
    51662 2 ""                                1 0
    51663 2 ""                                0 0
    51664 2 "EZETIMIBE; SIMVASTATIN"          0 0
    51665 2 ""                                0 0
    51666 2 ""                                0 0
    51667 2 "AMLODIPINE"                      0 0
    51667 2 "CHLORTHALIDONE"                  0 0
    51667 2 "SIMVASTATIN"                     0 0
    51668 2 "ALBUTEROL"                       0 0
    51668 2 "INSULIN ZINC"                    0 0
    51669 2 ""                                0 0
    51670 2 ""                                0 0
    51671 2 ""                                0 0
    51672 2 ""                                1 0
    51673 2 "ETHINYL ESTRADIOL; NORGESTIMATE" 1 0
    51674 2 ""                                1 0
    51675 2 "ALENDRONATE"                     0 0
    51675 2 "ATORVASTATIN"                    0 0
    51675 2 "LEVOTHYROXINE"                   0 0
    51675 2 "LISINOPRIL"                      0 0
    51675 2 "METOPROLOL"                      0 0
    end
    label values cycle _cycle
    label def _cycle 1 "2009-10" 2 "2011-12", modify

  • #2
    In your example data, each drug that is mentioned occurs only once. This points up in the extreme, a problem about the posing of this question. What do you want to do if there are several drugs tied for the greatest number of prescriptions?

    Comment


    • #3
      Each drug occurs once in this sample but in the data the drug will occur more than once. I'd like to identify most common drug among the subpopulation in each cycle then also the weighted percent. I don't necessarily have to take top 20, I just want to descending sort the list. I'd just report ties the same. However, when you weight, I am trying to figure out how to collapse so the subpopulation is accurate when I apply the weights and then how to accurately report the proportion of the population (weighted) most common drug for the subpopulation in each cycle.

      Comment


      • #4
        I just want to descending sort the list.
        Then just do this:
        Code:
        drop if !inAnalysis | missing(rxddrug)
        
        //  DESCENDING SORTED LIST OF DRUGS BY CYCLE
        by cycle rxddrug, sort: gen freq = _N
        egen flag = tag(cycle rxddrug)
        gsort flag cycle -freq
        list cycle rxddrug freq if flag, noobs clean
        
        //  DESCENDING SORTED LIST OF DRUGS BY CYCLE & SUBPOPULATION
        by cycle subpop rxddrug, sort: replace freq = _N
        drop flag
        egen flag = tag(cycle subpop rxddrug)
        gsort flag cycle subpop -freq
        list cycle subpop rxddrug freq if flag, noobs clean
        However, when you weight, I am trying to figure out how to collapse so the subpopulation is accurate when I apply the weights and then how to accurately report the proportion of the population (weighted) most common drug for the subpopulation in each cycle.
        I don't understand this sentence. There has been no previous mention of -collapse-, nor anything that I would see using -collapse-. If you do plan to use the -collapse- command, no worry, because -collapse- accepts pweights for most statistics. For the proportion of the population using different drugs, use the -svy: proportion- command with its -over()- option.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Then just do this:
          Code:
          drop if !inAnalysis | missing(rxddrug)
          
          // DESCENDING SORTED LIST OF DRUGS BY CYCLE
          by cycle rxddrug, sort: gen freq = _N
          egen flag = tag(cycle rxddrug)
          gsort flag cycle -freq
          list cycle rxddrug freq if flag, noobs clean
          
          // DESCENDING SORTED LIST OF DRUGS BY CYCLE & SUBPOPULATION
          by cycle subpop rxddrug, sort: replace freq = _N
          drop flag
          egen flag = tag(cycle subpop rxddrug)
          gsort flag cycle subpop -freq
          list cycle subpop rxddrug freq if flag, noobs clean

          I don't understand this sentence. There has been no previous mention of -collapse-, nor anything that I would see using -collapse-. If you do plan to use the -collapse- command, no worry, because -collapse- accepts pweights for most statistics. For the proportion of the population using different drugs, use the -svy: proportion- command with its -over()- option.
          What I meant to say is how to appropriately do this with sample weights after using the svyset command in STATA. From the sample above, my data is recorded as a long dataset with multiple drugs per individual (potentially) and some individuals having no drug use. To appropriately weight with the correct subpopulation using the svy command is what I'm trying to do to record the most common medications.

          Comment


          • #6
            It isn't very different with weights. The difference is that wherever the previous code refers to -by something: gen freq = _N-, you have to replace _N with the sum of the pweights of those observations.

            Code:
            drop if !inAnalysis | missing(rxddrug)
            
            // DESCENDING SORTED LIST OF DRUGS BY CYCLE
            by cycle rxddrug, sort: egen freq = total(pweight_variable)
            egen flag = tag(cycle rxddrug)
            gsort flag cycle -freq
            list cycle rxddrug freq if flag, noobs clean
            
            // DESCENDING SORTED LIST OF DRUGS BY CYCLE & SUBPOPULATION
            drop freq
            by cycle subpop rxddrug, sort: egen freq = total(pweight_variable)
            drop flag
            egen flag = tag(cycle subpop rxddrug)
            gsort flag cycle subpop -freq
            list cycle subpop rxddrug freq if flag, noobs clean
            You will have to consult the documentation for the data sets you are working with to determine which variable is the appropriate one to use where I have written pweight_variable.

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              It isn't very different with weights. The difference is that wherever the previous code refers to -by something: gen freq = _N-, you have to replace _N with the sum of the pweights of those observations.

              Code:
              drop if !inAnalysis | missing(rxddrug)
              
              // DESCENDING SORTED LIST OF DRUGS BY CYCLE
              by cycle rxddrug, sort: egen freq = total(pweight_variable)
              egen flag = tag(cycle rxddrug)
              gsort flag cycle -freq
              list cycle rxddrug freq if flag, noobs clean
              
              // DESCENDING SORTED LIST OF DRUGS BY CYCLE & SUBPOPULATION
              drop freq
              by cycle subpop rxddrug, sort: egen freq = total(pweight_variable)
              drop flag
              egen flag = tag(cycle subpop rxddrug)
              gsort flag cycle subpop -freq
              list cycle subpop rxddrug freq if flag, noobs clean
              You will have to consult the documentation for the data sets you are working with to determine which variable is the appropriate one to use where I have written pweight_variable.
              Thank you, Dr. Schechter for the feedback.

              I had 2 additional questions pertaining to the pweight you describe which may be rudimentary:

              1. What is the interpretation of the freq variable? Is that just the number of individuals within the subpopulation who report having used this medication? Does it matter that you have multiple lines per person or that an individual in the subpop has 0 recorded medications?
              1a. What if I want to report out proportions instead? Is there any easy way to do this in STATA?
              2. Is this possible to perform using the svyset command (e.g., svyset sdmvpsu [pweight=wtint2yr], strata(sdmvstra) vce(linearized)) or would this net the same result ultimately?


              Comment


              • #8
                1. The interpretation of the freq variable (whether in the weighted or unweighted analysis) is directly calculated as the number of reported prescriptions, not the number of individuals. In your example data, however, each individual only reports the same drug once, so the results would be the same.

                2. I think you can do this with:
                Code:
                gen byte one = 1 if !missing(rxddrug)
                svyset sdmvpsu [pweight=wtint2yr], strata(sdmvstra) vce(linearized)
                svy: total one, over(rxddrug)

                Comment


                • #9
                  Originally posted by Clyde Schechter View Post
                  1. The interpretation of the freq variable (whether in the weighted or unweighted analysis) is directly calculated as the number of reported prescriptions, not the number of individuals. In your example data, however, each individual only reports the same drug once, so the results would be the same.

                  2. I think you can do this with:
                  Code:
                  gen byte one = 1 if !missing(rxddrug)
                  svyset sdmvpsu [pweight=wtint2yr], strata(sdmvstra) vce(linearized)
                  svy: total one, over(rxddrug)
                  I suppose what I'm trying to get at is the most common medications reported for a subpopulation by cycle and over time. So, for example, among Men 20-44 who reported prescription drug use, what is the most common medication used? The command you reported above for the unweighted is fine. My question pertains moreso to the weighted observations; that is, among men 20-44 who report prescription drug use, what is the most common medication used (proportion)?

                  The code you mentioned does not allow me to do this over a string variable (rxddrug), and maybe I'm just new to STATA I'm not sure if collapse is appropriate in this situation and/or how to use the weights to generate the proportions among this subpopulation.

                  Comment


                  • #10
                    The code you mentioned does not allow me to do this over a string variable (rxddrug), and maybe I'm just new to STATA I'm not sure if collapse is appropriate in this situation and/or how to use the weights to generate the proportions among this subpopulation.
                    The -svy:- prefix has a -subpop()- option that allows you to designate subpopulations. Since your data example does not show any weights or age or sex variables, I cannot show you the actual code you would need to use. But if you can figure out an -if- condition that identifies men age 20-24, putting that in the -subpop()- option of -svy:- will allow you to that. In addition, if the various subpopulations you want are mutually exclusive, then you don't even need -subpop()-. You can just identify those subpopulations in a single variable (e.g. -egen subset = group(sex_variable agegroup_variable), label-) then you can just do a loop over the values of that subset variable, with -svy:total one, over(drug_variable)- inside the loop.

                    As for not being able to use rxddrug in the -over()- option because it is a string, you can get around that with:
                    Code:
                    encode rxddrug, gen(drug_variable)
                    and use drug_variable in its place.

                    By the way, I have the sense that every post brings a slightly different statement of your ultimate goal here. If you want proportions, not total counts, the -svy: proportion drug_variable- command will get you that directly.

                    Added: As you have alluded to -collapse- on a few occasions, you can also do it that way. If you are going to go the -collapse- route, there is no need to first -svyset-. -collapse- is not supported by the -svy:- prefix. But the statistics that -collapse- can compute for you do not require all of the information provided by -svyset-, only the pweight. So you can do:

                    Code:
                    gen byte one = 1
                    collapse (sum) freq = one [pweight = wtint2yr], by(drug_variable agegroup_variable sex_variable)
                    and then do the appropriate -sort- and -list- commands to get sorted lists.
                    Last edited by Clyde Schechter; 06 Nov 2022, 16:12.

                    Comment


                    • #11
                      Thank you again, Dr. Schechter.

                      To be more clear, I want to obtain the most common medications used by my subpopulation reported as proportions; for example, men 20-44. I created a variable called inAnalysis (=1 if they are part of my sample; 0, otherwise). I have a subpopulation also for men 20-44 that report high blood pressure (hbp=1; 0, otherwise). I also have a variable hbp010 that reports if they responded to the question at the time of the exam (1=yes; 2/3=no; rest, missing). I have multiple drugs reported for individuals (seqn) or 0 drugs reported (drug_variable=" ").

                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      clear
                      input double(seqn cycle) long drug_variable double(hbp gender hbp010)
                      51624 1  686 0 1 .
                      51624 1  410 0 1 .
                      51624 1  269 0 1 .
                      51625 1    . 0 1 .
                      51626 1    . 0 1 .
                      51627 1    . 0 1 .
                      51628 1  428 0 2 .
                      51628 1  551 0 2 .
                      51628 1  229 0 2 .
                      51628 1 1065 0 2 .
                      51628 1  693 0 2 .
                      51628 1  450 0 2 .
                      51629 1    . 0 1 .
                      51630 1  613 0 2 .
                      51630 1  585 0 2 .
                      51630 1  191 0 2 .
                      51630 1   26 0 2 .
                      51630 1  389 0 2 .
                      51631 1  395 0 2 .
                      51632 1  759 0 1 .
                      51633 1 1025 0 1 .
                      51634 1    . 0 1 .
                      51635 1  764 0 1 .
                      51635 1  836 0 1 .
                      51635 1   63 0 1 .
                      51635 1  992 0 1 .
                      51635 1 1025 0 1 .
                      51636 1    . 0 1 .
                      51637 1    . 0 2 3
                      51638 1    . 0 1 .
                      51639 1    . 0 1 .
                      51640 1  527 0 1 .
                      51640 1  783 0 1 .
                      51640 1  685 0 1 .
                      51640 1  354 0 1 .
                      51641 1    . 0 1 .
                      51642 1    . 0 1 .
                      51643 1  943 0 2 2
                      51643 1  915 0 2 2
                      51643 1  851 0 2 2
                      51643 1  370 0 2 2
                      51643 1  632 0 2 2
                      51643 1  201 0 2 2
                      51643 1  880 0 2 2
                      51643 1  191 0 2 2
                      51643 1 1095 0 2 2
                      51643 1  836 0 2 2
                      51643 1  714 0 2 2
                      51643 1  929 0 2 2
                      51643 1  762 0 2 2
                      51643 1  424 0 2 2
                      51643 1  415 0 2 2
                      51644 1  128 0 1 .
                      51645 1  764 0 1 .
                      51645 1  717 0 1 .
                      51646 1  185 0 1 .
                      51647 1  518 0 2 .
                      51647 1  191 0 2 .
                      51648 1    . 0 1 .
                      51649 1    . 0 2 .
                      51650 1    . 0 2 .
                      51651 1    . 0 2 .
                      51652 1    . 0 2 .
                      51653 1    . 0 1 .
                      51654 1  581 0 1 .
                      51655 1    . 0 2 .
                      51656 1  489 0 1 .
                      51657 1    . 0 1 .
                      51658 1   15 0 2 2
                      51659 1    . 0 2 .
                      51660 1    . 0 1 .
                      51661 1   50 0 2 .
                      51661 1 1065 0 2 .
                      51661 1  370 0 2 .
                      51661 1  173 0 2 .
                      51661 1  941 0 2 .
                      51661 1  613 0 2 .
                      51661 1  281 0 2 .
                      51661 1  762 0 2 .
                      51662 1    . 0 2 2
                      51663 1    . 0 2 .
                      51664 1  489 0 2 .
                      51665 1    . 0 2 .
                      51666 1    . 0 2 .
                      51667 1  261 0 1 .
                      51667 1   63 0 1 .
                      51667 1  992 0 1 .
                      51668 1   35 0 2 .
                      51668 1  641 0 2 .
                      51669 1    . 0 2 .
                      51670 1    . 0 1 .
                      51671 1    . 0 2 .
                      51672 1    . 0 2 2
                      51673 1  476 0 2 2
                      51674 1    . 0 2 3
                      51675 1  764 0 2 .
                      51675 1  705 0 2 .
                      51675 1  693 0 2 .
                      51675 1  114 0 2 .
                      51675 1   39 0 2 .
                      end
                      label values drug_variable drug_variable
                      label def drug_variable 15 "ACETAMINOPHEN; HYDROCODONE", modify
                      label def drug_variable 26 "ACYCLOVIR", modify
                      label def drug_variable 35 "ALBUTEROL", modify
                      label def drug_variable 39 "ALENDRONATE", modify
                      label def drug_variable 50 "ALPRAZOLAM", modify
                      label def drug_variable 63 "AMLODIPINE", modify
                      label def drug_variable 114 "ATORVASTATIN", modify
                      label def drug_variable 128 "AZITHROMYCIN", modify
                      label def drug_variable 173 "BROMAZEPAM", modify
                      label def drug_variable 185 "BUDESONIDE", modify
                      label def drug_variable 191 "BUPROPION", modify
                      label def drug_variable 201 "CALCIUM ACETATE", modify
                      label def drug_variable 229 "CARVEDILOL", modify
                      label def drug_variable 261 "CHLORTHALIDONE", modify
                      label def drug_variable 269 "CIMETIDINE", modify
                      label def drug_variable 281 "CLINDAMYCIN", modify
                      label def drug_variable 354 "DEXMETHYLPHENIDATE", modify
                      label def drug_variable 370 "DICYCLOMINE", modify
                      label def drug_variable 389 "DOCUSATE", modify
                      label def drug_variable 395 "DONT KNOW", modify
                      label def drug_variable 410 "DULOXETINE", modify
                      label def drug_variable 415 "EFAVIRENZ", modify
                      label def drug_variable 424 "EMTRICITABINE", modify
                      label def drug_variable 428 "ENALAPRIL; HYDROCHLOROTHIAZIDE", modify
                      label def drug_variable 450 "ESOMEPRAZOLE", modify
                      label def drug_variable 476 "ETHINYL ESTRADIOL; NORGESTIMATE", modify
                      label def drug_variable 489 "EZETIMIBE; SIMVASTATIN", modify
                      label def drug_variable 518 "FLUOXETINE", modify
                      label def drug_variable 527 "FLUTICASONE; SALMETEROL", modify
                      label def drug_variable 551 "GLIMEPIRIDE", modify
                      label def drug_variable 581 "HYDROCHLOROTHIAZIDE; LISINOPRIL", modify
                      label def drug_variable 585 "HYDROCHLOROTHIAZIDE; OLMESARTAN", modify
                      label def drug_variable 613 "IBUPROFEN", modify
                      label def drug_variable 632 "INSULIN GLARGINE", modify
                      label def drug_variable 641 "INSULIN ZINC", modify
                      label def drug_variable 685 "LEVALBUTEROL", modify
                      label def drug_variable 686 "LEVETIRACETAM", modify
                      label def drug_variable 693 "LEVOTHYROXINE", modify
                      label def drug_variable 705 "LISINOPRIL", modify
                      label def drug_variable 714 "LOSARTAN", modify
                      label def drug_variable 717 "LOVASTATIN", modify
                      label def drug_variable 759 "METHYLPHENIDATE", modify
                      label def drug_variable 762 "METOCLOPRAMIDE", modify
                      label def drug_variable 764 "METOPROLOL", modify
                      label def drug_variable 783 "MONTELUKAST", modify
                      label def drug_variable 836 "OMEPRAZOLE", modify
                      label def drug_variable 851 "OXCARBAZEPINE", modify
                      label def drug_variable 880 "PERPHENAZINE", modify
                      label def drug_variable 915 "PRAVASTATIN", modify
                      label def drug_variable 929 "PROPRANOLOL", modify
                      label def drug_variable 941 "RABEPRAZOLE", modify
                      label def drug_variable 943 "RALTEGRAVIR", modify
                      label def drug_variable 992 "SIMVASTATIN", modify
                      label def drug_variable 1025 "TAMSULOSIN", modify
                      label def drug_variable 1065 "TRAMADOL", modify
                      label def drug_variable 1095 "VALACYCLOVIR", modify

                      When I modify the STATA code above you shared, I get an unusual number reported for my population and subpopulation which indicate that it is counting the same person multiple times depending on how many drugs they use.
                      Observations: 20,088
                      Population size: 580,534,789
                      Subpopulation no. obs: 1,294
                      Subpop size: 50,903,835
                      Code:
                      encode rxddrug, gen(drug_variable)
                      gen byte one = 1 if !missing(drug_variable)
                      svyset sdmvpsu [pweight=wtint2yr], strata(sdmvstra) vce(linearized)
                      svy, subpop(if inAnalysis==1 & : total one, over(rxddrug)
                      I am attempting to get something similar to this:
                      Code:
                      svy, subpop(if inAnalysis==1 & hbp010!=.) : tab drug_variable, col ci
                      svy, subpop (if inAnalysis==1 & hbp010!=. & hbp==1): tab drug_variable, col ci
                      svy, subpop (if inAnalysis==1 & hbp010!=. & hbp==0): tab drug_variable, col ci
                      or
                      Code:
                      svyset sdmvpsu [pweight=wtint11yr], strata(sdmvstra) vce(linearized)
                      svy, subpop(if inAnalysis==1 & hbp010!=.) : tab cycle drug_variable, row ci
                      svy, subpop (if inAnalysis==1 & hbp010!=. & hbp==1): tab cycle drug_variable, row  se
                      svy, subpop (if inAnalysis==1 & hbp010!=. & hbp==0): tab cycle drug_variable, row  se

                      Comment


                      • #12
                        Look, we're never going to get anywhere with this if you post code and data examples that don't work with each other. You are asking for help with code that mentions variables like wtint11yr or wtint2yr, but there is no such variable in the example. Please post back with a -dataex- example that includes all of the variables needed to run the code you are asking for help with.

                        That said, you should be able to get what you want with -svy, subpop(if whatever): proportion drug_variable-.
                        Last edited by Clyde Schechter; 07 Nov 2022, 10:21.

                        Comment


                        • #13
                          Originally posted by Clyde Schechter View Post
                          Look, we're never going to get anywhere with this if you post code and data examples that don't work with each other. You are asking for help with code that mentions variables like wtint11yr or wtint2yr, but there is no such variable in the example. Please post back with a -dataex- example that includes all of the variables needed to run the code you are asking for help with.

                          That said, you should be able to get what you want with -svy, subpop(if whatever): proportion drug_variable-.
                          My apologies, I forgot to include the weight variable in my dataex example:
                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input double(seqn cycle) long drug_variable double(hbp gender hbp010 wtint2yr wtint11yr)
                          51624 1 439 0 1 . 80100.543512   14303.6689453125
                          51624 1 271 0 1 . 80100.543512   14303.6689453125
                          51624 1 179 0 1 . 80100.543512   14303.6689453125
                          51625 1   . 0 1 . 53901.104285     9625.197265625
                          51626 1   . 0 1 . 13953.078343      2491.62109375
                          51627 1   . 0 1 . 11664.899398  2083.017822265625
                          51628 1 281 0 2 . 20090.339256     3587.560546875
                          51628 1 353 0 2 . 20090.339256     3587.560546875
                          51628 1 146 0 2 . 20090.339256     3587.560546875
                          51628 1 679 0 2 . 20090.339256     3587.560546875
                          51628 1 443 0 2 . 20090.339256     3587.560546875
                          51628 1 291 0 2 . 20090.339256     3587.560546875
                          51629 1   . 0 1 . 22537.826997  4024.612060546875
                          51630 1 394 0 2 . 74212.269993     13252.19140625
                          51630 1 375 0 2 . 74212.269993     13252.19140625
                          51630 1 123 0 2 . 74212.269993     13252.19140625
                          51630 1  15 0 2 . 74212.269993     13252.19140625
                          51630 1 260 0 2 . 74212.269993     13252.19140625
                          51631 1 264 0 2 . 23306.397744   4161.85693359375
                          51632 1 481 0 1 .  8056.943427 1438.7398681640625
                          51633 1 653 0 1 . 11998.401196  2142.571533203125
                          51634 1   . 0 1 .  9805.508008 1750.9835205078125
                          51635 1 486 0 1 . 21806.929002  3894.094482421875
                          51635 1 539 0 1 . 21806.929002  3894.094482421875
                          51635 1  40 0 1 . 21806.929002  3894.094482421875
                          51635 1 634 0 1 . 21806.929002  3894.094482421875
                          51635 1 653 0 1 . 21806.929002  3894.094482421875
                          51636 1   . 0 1 . 11466.690658  2047.623291015625
                          51637 1   . 0 2 3 70234.243007   12541.8291015625
                          51638 1   . 0 1 .  29727.78403   5308.53271484375
                          51639 1   . 0 1 .   9011.45848   1609.18896484375
                          51640 1 340 0 1 . 11445.166885   2043.77978515625
                          51640 1 501 0 1 . 11445.166885   2043.77978515625
                          51640 1 438 0 1 . 11445.166885   2043.77978515625
                          51640 1 240 0 1 . 11445.166885   2043.77978515625
                          51641 1   . 0 1 . 14703.667116   2625.65478515625
                          51642 1   . 0 1 .   8120.21468  1450.038330078125
                          51643 1 608 0 2 2  39087.02305   6979.82568359375
                          51643 1 585 0 2 2  39087.02305   6979.82568359375
                          51643 1 548 0 2 2  39087.02305   6979.82568359375
                          51643 1 250 0 2 2  39087.02305   6979.82568359375
                          51643 1 405 0 2 2  39087.02305   6979.82568359375
                          51643 1 131 0 2 2  39087.02305   6979.82568359375
                          51643 1 564 0 2 2  39087.02305   6979.82568359375
                          51643 1 123 0 2 2  39087.02305   6979.82568359375
                          51643 1 700 0 2 2  39087.02305   6979.82568359375
                          51643 1 539 0 2 2  39087.02305   6979.82568359375
                          51643 1 455 0 2 2  39087.02305   6979.82568359375
                          51643 1 599 0 2 2  39087.02305   6979.82568359375
                          51643 1 484 0 2 2  39087.02305   6979.82568359375
                          51643 1 278 0 2 2  39087.02305   6979.82568359375
                          51643 1 275 0 2 2  39087.02305   6979.82568359375
                          51644 1  83 0 1 . 24620.921662   4396.59326171875
                          51645 1 486 0 1 .  9286.357078  1658.278076171875
                          51645 1 457 0 1 .  9286.357078  1658.278076171875
                          51646 1 118 0 1 . 39300.030288   7017.86279296875
                          51647 1 333 0 2 . 86635.796639    15470.677734375
                          51647 1 123 0 2 . 86635.796639    15470.677734375
                          51648 1   . 0 1 . 23137.127541    4131.6298828125
                          51649 1   . 0 2 . 14322.938135   2557.66748046875
                          51650 1   . 0 2 . 10278.829195 1835.5052490234375
                          51651 1   . 0 2 . 18878.317173  3371.128173828125
                          51652 1   . 0 2 . 10555.096448  1884.838623046875
                          51653 1   . 0 1 . 90639.846079   16185.6865234375
                          51654 1 371 0 1 . 56052.778153   10009.4248046875
                          51655 1   . 0 2 . 18366.596441  3279.749267578125
                          51656 1 314 0 1 . 99572.381021    17780.783203125
                          51657 1   . 0 1 .  16965.18451  3029.497314453125
                          51658 1   7 0 2 2 16111.938368    2877.1318359375
                          51659 1   . 0 2 . 36919.199463   6592.71435546875
                          51660 1   . 0 1 . 23610.845803      4216.22265625
                          51661 1  30 0 2 .  6017.788326 1074.6051025390625
                          51661 1 679 0 2 .  6017.788326 1074.6051025390625
                          51661 1 250 0 2 .  6017.788326 1074.6051025390625
                          51661 1 109 0 2 .  6017.788326 1074.6051025390625
                          51661 1 606 0 2 .  6017.788326 1074.6051025390625
                          51661 1 394 0 2 .  6017.788326 1074.6051025390625
                          51661 1 190 0 2 .  6017.788326 1074.6051025390625
                          51661 1 484 0 2 .  6017.788326 1074.6051025390625
                          51662 1   . 0 2 2 17546.485589  3133.301025390625
                          51663 1   . 0 2 . 13887.875503  2479.977783203125
                          51664 1 314 0 2 . 78599.232218   14035.5771484375
                          51665 1   . 0 2 . 13382.517936    2389.7353515625
                          51666 1   . 0 2 .  9520.304491 1700.0543212890625
                          51667 1 173 0 1 . 77981.054133   13925.1884765625
                          51667 1  40 0 1 . 77981.054133   13925.1884765625
                          51667 1 634 0 1 . 77981.054133   13925.1884765625
                          51668 1  19 0 2 . 12778.267582   2281.83349609375
                          51668 1 412 0 2 . 12778.267582   2281.83349609375
                          51669 1   . 0 2 .  6068.271717 1083.6199951171875
                          51670 1   . 0 1 .  28070.51835    5012.5927734375
                          51671 1   . 0 2 . 10960.843032   1957.29345703125
                          51672 1   . 0 2 2 88885.975292   15872.4951171875
                          51673 1 306 0 2 2 21345.787016  3811.747802734375
                          51674 1   . 0 2 3 74719.574423        13342.78125
                          51675 1 486 0 2 . 22147.722941  3954.950439453125
                          51675 1 448 0 2 . 22147.722941  3954.950439453125
                          51675 1 443 0 2 . 22147.722941  3954.950439453125
                          51675 1  76 0 2 . 22147.722941  3954.950439453125
                          51675 1  22 0 2 . 22147.722941  3954.950439453125
                          end
                          label values drug_variable drug_variable
                          label def drug_variable 7 "ACETAMINOPHEN; HYDROCODONE", modify
                          label def drug_variable 15 "ACYCLOVIR", modify
                          label def drug_variable 19 "ALBUTEROL", modify
                          label def drug_variable 22 "ALENDRONATE", modify
                          label def drug_variable 30 "ALPRAZOLAM", modify
                          label def drug_variable 40 "AMLODIPINE", modify
                          label def drug_variable 76 "ATORVASTATIN", modify
                          label def drug_variable 83 "AZITHROMYCIN", modify
                          label def drug_variable 109 "BROMAZEPAM", modify
                          label def drug_variable 118 "BUDESONIDE", modify
                          label def drug_variable 123 "BUPROPION", modify
                          label def drug_variable 131 "CALCIUM ACETATE", modify
                          label def drug_variable 146 "CARVEDILOL", modify
                          label def drug_variable 173 "CHLORTHALIDONE", modify
                          label def drug_variable 179 "CIMETIDINE", modify
                          label def drug_variable 190 "CLINDAMYCIN", modify
                          label def drug_variable 240 "DEXMETHYLPHENIDATE", modify
                          label def drug_variable 250 "DICYCLOMINE", modify
                          label def drug_variable 260 "DOCUSATE", modify
                          label def drug_variable 264 "DONT KNOW", modify
                          label def drug_variable 271 "DULOXETINE", modify
                          label def drug_variable 275 "EFAVIRENZ", modify
                          label def drug_variable 278 "EMTRICITABINE", modify
                          label def drug_variable 281 "ENALAPRIL; HYDROCHLOROTHIAZIDE", modify
                          label def drug_variable 291 "ESOMEPRAZOLE", modify
                          label def drug_variable 306 "ETHINYL ESTRADIOL; NORGESTIMATE", modify
                          label def drug_variable 314 "EZETIMIBE; SIMVASTATIN", modify
                          label def drug_variable 333 "FLUOXETINE", modify
                          label def drug_variable 340 "FLUTICASONE; SALMETEROL", modify
                          label def drug_variable 353 "GLIMEPIRIDE", modify
                          label def drug_variable 371 "HYDROCHLOROTHIAZIDE; LISINOPRIL", modify
                          label def drug_variable 375 "HYDROCHLOROTHIAZIDE; OLMESARTAN", modify
                          label def drug_variable 394 "IBUPROFEN", modify
                          label def drug_variable 405 "INSULIN GLARGINE", modify
                          label def drug_variable 412 "INSULIN ZINC", modify
                          label def drug_variable 438 "LEVALBUTEROL", modify
                          label def drug_variable 439 "LEVETIRACETAM", modify
                          label def drug_variable 443 "LEVOTHYROXINE", modify
                          label def drug_variable 448 "LISINOPRIL", modify
                          label def drug_variable 455 "LOSARTAN", modify
                          label def drug_variable 457 "LOVASTATIN", modify
                          label def drug_variable 481 "METHYLPHENIDATE", modify
                          label def drug_variable 484 "METOCLOPRAMIDE", modify
                          label def drug_variable 486 "METOPROLOL", modify
                          label def drug_variable 501 "MONTELUKAST", modify
                          label def drug_variable 539 "OMEPRAZOLE", modify
                          label def drug_variable 548 "OXCARBAZEPINE", modify
                          label def drug_variable 564 "PERPHENAZINE", modify
                          label def drug_variable 585 "PRAVASTATIN", modify
                          label def drug_variable 599 "PROPRANOLOL", modify
                          label def drug_variable 606 "RABEPRAZOLE", modify
                          label def drug_variable 608 "RALTEGRAVIR", modify
                          label def drug_variable 634 "SIMVASTATIN", modify
                          label def drug_variable 653 "TAMSULOSIN", modify
                          label def drug_variable 679 "TRAMADOL", modify
                          label def drug_variable 700 "VALACYCLOVIR", modify

                          The issue I have with performing -svy, subpop(if whatever): proportion drug_variable-, is that the population size and subpop size considers the same individual multiple times; therefore, the number of observations, population size, subpop size are all considered multiple times depending on how many times seqn appears. Right now, the wtint11yr can be disregarded since the dataex example only outputs from cycle 1. My intention is to use multiple cycles after troubleshooting with only 1 cycle.


                          Click image for larger version

Name:	example.JPG
Views:	1
Size:	48.6 KB
ID:	1688464

                          Comment


                          • #14
                            OK, I see the problem. This is one of those unusual situations in Stata where we need to go to wide layout having a dichotomous 0/1 variable for each drug indicating whether it was prescribed for that person, with each person having only one observation. And to do that, we will have to revert to the string variable (originally called rxddrug), but suitably modified so that it can become a variable name.

                            Code:
                            * Example generated by -dataex-. For more info, type help dataex
                            clear*
                            input double(seqn cycle) long drug_variable double(hbp gender hbp010 wtint2yr wtint11yr)
                            51624 1 439 0 1 . 80100.543512   14303.6689453125
                            51624 1 271 0 1 . 80100.543512   14303.6689453125
                            51624 1 179 0 1 . 80100.543512   14303.6689453125
                            51625 1   . 0 1 . 53901.104285     9625.197265625
                            51626 1   . 0 1 . 13953.078343      2491.62109375
                            51627 1   . 0 1 . 11664.899398  2083.017822265625
                            51628 1 281 0 2 . 20090.339256     3587.560546875
                            51628 1 353 0 2 . 20090.339256     3587.560546875
                            51628 1 146 0 2 . 20090.339256     3587.560546875
                            51628 1 679 0 2 . 20090.339256     3587.560546875
                            51628 1 443 0 2 . 20090.339256     3587.560546875
                            51628 1 291 0 2 . 20090.339256     3587.560546875
                            51629 1   . 0 1 . 22537.826997  4024.612060546875
                            51630 1 394 0 2 . 74212.269993     13252.19140625
                            51630 1 375 0 2 . 74212.269993     13252.19140625
                            51630 1 123 0 2 . 74212.269993     13252.19140625
                            51630 1  15 0 2 . 74212.269993     13252.19140625
                            51630 1 260 0 2 . 74212.269993     13252.19140625
                            51631 1 264 0 2 . 23306.397744   4161.85693359375
                            51632 1 481 0 1 .  8056.943427 1438.7398681640625
                            51633 1 653 0 1 . 11998.401196  2142.571533203125
                            51634 1   . 0 1 .  9805.508008 1750.9835205078125
                            51635 1 486 0 1 . 21806.929002  3894.094482421875
                            51635 1 539 0 1 . 21806.929002  3894.094482421875
                            51635 1  40 0 1 . 21806.929002  3894.094482421875
                            51635 1 634 0 1 . 21806.929002  3894.094482421875
                            51635 1 653 0 1 . 21806.929002  3894.094482421875
                            51636 1   . 0 1 . 11466.690658  2047.623291015625
                            51637 1   . 0 2 3 70234.243007   12541.8291015625
                            51638 1   . 0 1 .  29727.78403   5308.53271484375
                            51639 1   . 0 1 .   9011.45848   1609.18896484375
                            51640 1 340 0 1 . 11445.166885   2043.77978515625
                            51640 1 501 0 1 . 11445.166885   2043.77978515625
                            51640 1 438 0 1 . 11445.166885   2043.77978515625
                            51640 1 240 0 1 . 11445.166885   2043.77978515625
                            51641 1   . 0 1 . 14703.667116   2625.65478515625
                            51642 1   . 0 1 .   8120.21468  1450.038330078125
                            51643 1 608 0 2 2  39087.02305   6979.82568359375
                            51643 1 585 0 2 2  39087.02305   6979.82568359375
                            51643 1 548 0 2 2  39087.02305   6979.82568359375
                            51643 1 250 0 2 2  39087.02305   6979.82568359375
                            51643 1 405 0 2 2  39087.02305   6979.82568359375
                            51643 1 131 0 2 2  39087.02305   6979.82568359375
                            51643 1 564 0 2 2  39087.02305   6979.82568359375
                            51643 1 123 0 2 2  39087.02305   6979.82568359375
                            51643 1 700 0 2 2  39087.02305   6979.82568359375
                            51643 1 539 0 2 2  39087.02305   6979.82568359375
                            51643 1 455 0 2 2  39087.02305   6979.82568359375
                            51643 1 599 0 2 2  39087.02305   6979.82568359375
                            51643 1 484 0 2 2  39087.02305   6979.82568359375
                            51643 1 278 0 2 2  39087.02305   6979.82568359375
                            51643 1 275 0 2 2  39087.02305   6979.82568359375
                            51644 1  83 0 1 . 24620.921662   4396.59326171875
                            51645 1 486 0 1 .  9286.357078  1658.278076171875
                            51645 1 457 0 1 .  9286.357078  1658.278076171875
                            51646 1 118 0 1 . 39300.030288   7017.86279296875
                            51647 1 333 0 2 . 86635.796639    15470.677734375
                            51647 1 123 0 2 . 86635.796639    15470.677734375
                            51648 1   . 0 1 . 23137.127541    4131.6298828125
                            51649 1   . 0 2 . 14322.938135   2557.66748046875
                            51650 1   . 0 2 . 10278.829195 1835.5052490234375
                            51651 1   . 0 2 . 18878.317173  3371.128173828125
                            51652 1   . 0 2 . 10555.096448  1884.838623046875
                            51653 1   . 0 1 . 90639.846079   16185.6865234375
                            51654 1 371 0 1 . 56052.778153   10009.4248046875
                            51655 1   . 0 2 . 18366.596441  3279.749267578125
                            51656 1 314 0 1 . 99572.381021    17780.783203125
                            51657 1   . 0 1 .  16965.18451  3029.497314453125
                            51658 1   7 0 2 2 16111.938368    2877.1318359375
                            51659 1   . 0 2 . 36919.199463   6592.71435546875
                            51660 1   . 0 1 . 23610.845803      4216.22265625
                            51661 1  30 0 2 .  6017.788326 1074.6051025390625
                            51661 1 679 0 2 .  6017.788326 1074.6051025390625
                            51661 1 250 0 2 .  6017.788326 1074.6051025390625
                            51661 1 109 0 2 .  6017.788326 1074.6051025390625
                            51661 1 606 0 2 .  6017.788326 1074.6051025390625
                            51661 1 394 0 2 .  6017.788326 1074.6051025390625
                            51661 1 190 0 2 .  6017.788326 1074.6051025390625
                            51661 1 484 0 2 .  6017.788326 1074.6051025390625
                            51662 1   . 0 2 2 17546.485589  3133.301025390625
                            51663 1   . 0 2 . 13887.875503  2479.977783203125
                            51664 1 314 0 2 . 78599.232218   14035.5771484375
                            51665 1   . 0 2 . 13382.517936    2389.7353515625
                            51666 1   . 0 2 .  9520.304491 1700.0543212890625
                            51667 1 173 0 1 . 77981.054133   13925.1884765625
                            51667 1  40 0 1 . 77981.054133   13925.1884765625
                            51667 1 634 0 1 . 77981.054133   13925.1884765625
                            51668 1  19 0 2 . 12778.267582   2281.83349609375
                            51668 1 412 0 2 . 12778.267582   2281.83349609375
                            51669 1   . 0 2 .  6068.271717 1083.6199951171875
                            51670 1   . 0 1 .  28070.51835    5012.5927734375
                            51671 1   . 0 2 . 10960.843032   1957.29345703125
                            51672 1   . 0 2 2 88885.975292   15872.4951171875
                            51673 1 306 0 2 2 21345.787016  3811.747802734375
                            51674 1   . 0 2 3 74719.574423        13342.78125
                            51675 1 486 0 2 . 22147.722941  3954.950439453125
                            51675 1 448 0 2 . 22147.722941  3954.950439453125
                            51675 1 443 0 2 . 22147.722941  3954.950439453125
                            51675 1  76 0 2 . 22147.722941  3954.950439453125
                            51675 1  22 0 2 . 22147.722941  3954.950439453125
                            end
                            label values drug_variable drug_variable
                            label def drug_variable 7 "ACETAMINOPHEN; HYDROCODONE", modify
                            label def drug_variable 15 "ACYCLOVIR", modify
                            label def drug_variable 19 "ALBUTEROL", modify
                            label def drug_variable 22 "ALENDRONATE", modify
                            label def drug_variable 30 "ALPRAZOLAM", modify
                            label def drug_variable 40 "AMLODIPINE", modify
                            label def drug_variable 76 "ATORVASTATIN", modify
                            label def drug_variable 83 "AZITHROMYCIN", modify
                            label def drug_variable 109 "BROMAZEPAM", modify
                            label def drug_variable 118 "BUDESONIDE", modify
                            label def drug_variable 123 "BUPROPION", modify
                            label def drug_variable 131 "CALCIUM ACETATE", modify
                            label def drug_variable 146 "CARVEDILOL", modify
                            label def drug_variable 173 "CHLORTHALIDONE", modify
                            label def drug_variable 179 "CIMETIDINE", modify
                            label def drug_variable 190 "CLINDAMYCIN", modify
                            label def drug_variable 240 "DEXMETHYLPHENIDATE", modify
                            label def drug_variable 250 "DICYCLOMINE", modify
                            label def drug_variable 260 "DOCUSATE", modify
                            label def drug_variable 264 "DONT KNOW", modify
                            label def drug_variable 271 "DULOXETINE", modify
                            label def drug_variable 275 "EFAVIRENZ", modify
                            label def drug_variable 278 "EMTRICITABINE", modify
                            label def drug_variable 281 "ENALAPRIL; HYDROCHLOROTHIAZIDE", modify
                            label def drug_variable 291 "ESOMEPRAZOLE", modify
                            label def drug_variable 306 "ETHINYL ESTRADIOL; NORGESTIMATE", modify
                            label def drug_variable 314 "EZETIMIBE; SIMVASTATIN", modify
                            label def drug_variable 333 "FLUOXETINE", modify
                            label def drug_variable 340 "FLUTICASONE; SALMETEROL", modify
                            label def drug_variable 353 "GLIMEPIRIDE", modify
                            label def drug_variable 371 "HYDROCHLOROTHIAZIDE; LISINOPRIL", modify
                            label def drug_variable 375 "HYDROCHLOROTHIAZIDE; OLMESARTAN", modify
                            label def drug_variable 394 "IBUPROFEN", modify
                            label def drug_variable 405 "INSULIN GLARGINE", modify
                            label def drug_variable 412 "INSULIN ZINC", modify
                            label def drug_variable 438 "LEVALBUTEROL", modify
                            label def drug_variable 439 "LEVETIRACETAM", modify
                            label def drug_variable 443 "LEVOTHYROXINE", modify
                            label def drug_variable 448 "LISINOPRIL", modify
                            label def drug_variable 455 "LOSARTAN", modify
                            label def drug_variable 457 "LOVASTATIN", modify
                            label def drug_variable 481 "METHYLPHENIDATE", modify
                            label def drug_variable 484 "METOCLOPRAMIDE", modify
                            label def drug_variable 486 "METOPROLOL", modify
                            label def drug_variable 501 "MONTELUKAST", modify
                            label def drug_variable 539 "OMEPRAZOLE", modify
                            label def drug_variable 548 "OXCARBAZEPINE", modify
                            label def drug_variable 564 "PERPHENAZINE", modify
                            label def drug_variable 585 "PRAVASTATIN", modify
                            label def drug_variable 599 "PROPRANOLOL", modify
                            label def drug_variable 606 "RABEPRAZOLE", modify
                            label def drug_variable 608 "RALTEGRAVIR", modify
                            label def drug_variable 634 "SIMVASTATIN", modify
                            label def drug_variable 653 "TAMSULOSIN", modify
                            label def drug_variable 679 "TRAMADOL", modify
                            label def drug_variable 700 "VALACYCLOVIR", modify
                            
                            svyset [pweight= wtint2yr]
                            
                            decode drug_variable, gen(rxddrug)
                            replace rxddrug = strtoname(rxddrug)
                            drop drug_variable
                            drop if missing( rxddrug)
                            quietly levelsof rxddrug, local(drugs) clean
                            
                            
                            gen _ = 1
                            reshape wide _, i(seqn) j(rxddrug) string
                            mvencode _*, mv(0)
                            rename _* *
                            
                            frame create results str32 drug float prop
                            
                            foreach v of varlist `drugs' {
                                svy, subpop(if !missing(hbp010)): proportion `v'
                                matrix M = r(table)
                                frame post results ("`v'") (M["b", "1.`v'"])
                            }
                            
                            frame change results
                            gsort -prop
                            list, noobs clean
                            And, of course, you can modify the -subpop()- option to meet your actual needs.

                            Comment


                            • #15
                              Thank you, Dr. Schechter; this is exactly what I encountered and wanted to see if there was a way using a long dataset approach instead of reshaping.

                              One additional query: As you might imagine, there are medications with unusually long names. Is there a way in the code below to modify so that I keep only the first 3-5 letters if it contains a ; in the drug name to circumvent the issue where: _ABACAVIR__DOLUTEGRAVIR__LAMIVUDI invalid variable name

                              svyset [pweight= wtint2yr] decode drug_variable, gen(rxddrug) replace rxddrug = strtoname(rxddrug) drop drug_variable drop if missing( rxddrug) quietly levelsof rxddrug, local(drugs) clean gen _ = 1 reshape wide _, i(seqn) j(rxddrug) string mvencode _*, mv(0) rename _* * frame create results str32 drug float prop foreach v of varlist `drugs' { svy, subpop(if !missing(hbp010)): proportion `v' matrix M = r(table) frame post results ("`v'") (M["b", "1.`v'"]) } frame change results gsort -prop list, noobs clean

                              Comment

                              Working...
                              X