Announcement

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

  • Unable to define value labels

    Sorry for pasting a long code here, and I was in a little quandary for a while, but still decided to post this question which may seem a bit silly.

    Assuming I have a variable of drugs coded in 4-digit numbers (e.g., 1101, 7100...), and I need to create value labels to interpret them back to the actual drug names (e.g., Codeine, Cannabinoids...). The code is attached.

    I generated the following codes but something weird happened. STATA kept reporting error of "invalid syntax" but I was unable to capture any grammatical errors or misspellings. I scrutinize each line and make sure each line ends with three slashes preceded by a blank. The code is followed by a detailed description of the issue.

    Code:
    /* ====================== Define and apply drug value labels ====================== */
    label define Prim_drug                    -1 "Not applicable" ///
                                            0 "Not stated" ///
                                            1100 "Organic Opiate Analgesics" ///
                                            1101 "Codeine" ///
                                            1102 "Morphine" ///
                                            1199 "Organic" ///
                                            1200 "Semisynthetic Opiate Analgesics" ///
                                            1201 "Buprenorphine" ///
                                            1202 "Heroin" ///
                                            1203 "Oxycodone" ///
                                            1298 "Buprenorphine or Naloxone" ///
                                            1299 "Semisynthetic Opiate Analgesics NEC" ///
                                            1300 "Synthetic Opiate Analgesics" ///
                                            1301 "Fentanyl" ///
                                            1303 "Levomethadyl" ///
                                            1304 "Meperidine" ///
                                            1305 "Methadone" ///
                                            1306 "Pethidine" ///
                                            1307 "Tramadol" ///
                                            1399 "Synthetic" ///
                                            1400 "Non Opiate Analgesics" ///
                                            1401 "Acetylsalicylic" ///
                                            1402 "Paracetamol" ///
                                            1403 "Ibuprofen" ///
                                            1499 "Non Opiate Analgesics NEC" ///
                                            2100 "Alcohols" ///
                                            2101 "Alcohol" ///
                                            2102 "Methanol" ///
                                            2199 "Alcohols NEC" ///
                                            2200 "Anaesthetics" ///
                                            2201 "Gamma-hydroxybutyrate" ///
                                            2202 "Ketamine" ///
                                            2203 "Nitrous oxide" ///
                                            2204 "Phencyclidine" ///
                                            2205 "Propofol" ///
                                            2299 "Anaesthetics" ///
                                            2300 "Barbiturates" ///
                                            2301 "Amylobarbitone" ///
                                            2302 "Methylphenobarbitone" ///
                                            2303 "Phenobarbitone" ///
                                            2399 "Barbiturates" ///
                                            2400 "Benzodiazepines," ///
                                            2401 "Alprazolam" ///
                                            2402 "Clonazepam" ///
                                            2403 "Diazepam" ///
                                            2404 "Flunitrazepam" ///
                                            2405 "Lorazepam" ///
                                            2406 "Nitrazepam" ///
                                            2407 "Oxazepam" ///
                                            2408 "Temazepam" ///
                                            2499 "Benzodiazepines," ///
                                            2501 "Gamma-hydroxybutyrate" ///
                                            2502 "Gamma-butyrolactone" ///
                                            2503 "Butanediol" ///
                                            2599 "GHB NEC" ///
                                            2901 "Chlormethiazole" ///
                                            2902 "Kava lactones" ///
                                            2903 "Zopiclone" ///
                                            2904 "Doxylamine" ///
                                            2905 "Promethazine" ///
                                            2906 "Zolpidem" ///
                                            2999 "Other sedatives and hypnotics" ///
                                            3100 "Amphetamines NFD" ///
                                            3101 "Amphetamine" ///
                                            3102 "Dexamphetamine" ///
                                            3103 "Methamphetamine" ///
                                            3104 "Amphetamine" ///
                                            3199 "Amphetamines" ///
                                            3200 "Cannabinoids" ///
                                            3201 "Cannabinoids" ///
                                            3300 "Ephedra" ///
                                            3301 "Ephedrine" ///
                                            3302 "Norephedrine" ///
                                            3303 "Pseudoephedrine" ///
                                            3399 "Ephedra" ///
                                            3400 "Phenethylamines" ///
                                            3401 "D.O.B." ///
                                            3402 "D.O.M." ///
                                            3403 "M.D.A." ///
                                            3404 "M.D.E.A." ///
                                            3405 "MDMA/Ectasy" ///
                                            3406 "Mescaline" ///
                                            3407 "P.M.A." ///
                                            3408 "T.M.A." ///
                                            3411 "D.O.I" ///
                                            3412 "PMMA" ///
                                            3413 "2C-B" ///
                                            3414 "Phenethylamine analogues" ///
                                            3499 "Phenethylamines" ///
                                            3500 "Tryptamines" ///
                                            3501 "Atropinic" ///
                                            3502 "Diethyltryptamine" ///
                                            3503 "Dimethyltryptamine" ///
                                            3504 "Lysergic" ///
                                            3505 "Psilocybin" ///
                                            3506 "Tryptamine" ///
                                            3599 "Tryptamines" ///
                                            3600 "Volatile nitrates" ///
                                            3601 "Amyl nitrates" ///
                                            3602 "Butyl nitrates" ///
                                            3699 "Volatile nitrates NEC" ///
                                            3700 "Cathinones" ///
                                            3701 "Cathinone" ///
                                            3702 "Methcathinone" ///
                                            3703 "Cathinone" ///
                                            3799 "Cathinones," ///
                                            3800 "Piperazines" ///
                                            3801 "1-Benzylpiperazine" ///
                                            3802 "1-(3-Trifluoromethylphenyl)-piperazine" ///
                                            3803 "1-(3-Chlorophenyl)-piperazine" ///
                                            3804 "Phenylpiperazine" ///
                                            3899 "Piperazines NEC" ///
                                            3900 "Other stimulants and hallucinogens" ///
                                            3901 "Caffeine" ///
                                            3902 "Cathinone" ///
                                            3903 "Cocaine" ///
                                            3904 "Methcathinone" ///
                                            3905 "Methylphenidate" ///
                                            3906 "Nicotine" ///
                                            3999 "Other stimulants and hallucinogens" ///
                                            4000 "Anabolic agents" ///
                                            4100 "Anabolic androgenic steroids" ///
                                            4101 "Boldenone" ///
                                            4102 "Dehydroepiandrosterone" ///
                                            4103 "Fluoxymesterone" ///
                                            4104 "Mesterolone" ///
                                            4105 "Methandriol" ///
                                            4106 "Methenolone" ///
                                            4107 "Nandrolone" ///
                                            4108 "Oxandrolone" ///
                                            4111 "Stanozolol" ///
                                            4112 "Testosterone" ///
                                            4199 "Anabolic androgenic steroids NEC" ///
                                            4200 "Beta2 agonists" ///
                                            4201 "Eformoterol" ///
                                            4202 "Fenoterol" ///
                                            4203 "Salbutamol" ///
                                            4299 "Beta2 agonists NEC" ///
                                            4300 "Peptide hormones mimetics" ///
                                            4301 "Chorionic gonadotrophin" ///
                                            4302 "Corticotrophin" ///
                                            4303 "Erythropoietin" ///
                                            4304 "Growth hormones" ///
                                            4305 "Insulin" ///
                                            4399 "Peptide" ///
                                            4900 "Other anabolic agents" ///
                                            4901 "Sulfonylurea hypoglycaemic agents" ///
                                            4902 "Tamoxifen" ///
                                            4903 "Thyroxine" ///
                                            4999 "Other anabolic agents" ///
                                            5000 "Antidepressants and antipsychotics" ///
                                            5100 "Monoamine oxidase inhibitors" ///
                                            5101 "Moclobemide" ///
                                            5102 "Phenelzine" ///
                                            5103 "Tranylcypromine" ///
                                            5199 "Monoamine" ///
                                            5200 "Phenothiazines" ///
                                            5201 "Chlorpromazine" ///
                                            5202 "Fluphenazine" ///
                                            5203 "Pericyazine" ///
                                            5204 "Thioridazine" ///
                                            5205 "Trifluoperazin" ///
                                            5299 "Phenothiazines," ///
                                            5300 "Serotonin reuptake inhibitors" ///
                                            5301 "Citalopram" ///
                                            5302 "Fluoxetine" ///
                                            5303 "Paroxetine" ///
                                            5304 "Sertraline" ///
                                            5399 "Serotonin reuptake inhibitors NEC" ///
                                            5400 "Thioxanthenes" ///
                                            5401 "Flupenthixol" ///
                                            5402 "Thiothixene" ///
                                            5499 "Thioxanthenes" ///
                                            5500 "Tricyclic antidepressants" ///
                                            5501 "Amitriptyline" ///
                                            5502 "Clomipramine" ///
                                            5503 "Dothiepin" ///
                                            5504 "Doxepin" ///
                                            5505 "Nortriptyline" ///
                                            5599 "Tricyclic" ///
                                            5600 "Atypical antipsychotics" ///
                                            5601 "Amisulpride" ///
                                            5602 "Aripriprazole" ///
                                            5603 "Clozapine" ///
                                            5604 "Olanzapine" ///
                                            5605 "Quetiapine" ///
                                            5606 "Risperidone" ///
                                            5607 "Ziprasidone" ///
                                            5699 "Atypical antipsychotics NEC" ///
                                            5901 "Butyrophenones" ///
                                            5902 "Lithium" ///
                                            5903 "Mianserin" ///
                                            5999 "Other" ///
                                            6000 "Volatile solvents" ///
                                            6100 "Aliphatic" ///
                                            6101 "Butane" ///
                                            6102 "Petroleum" ///
                                            6103 "Propane" ///
                                            6199 "Aliphatic" ///
                                            6200 "Aromatic" ///
                                            6201 "Toluene" ///
                                            6202 "Xylene" ///
                                            6299 "Aromatic hydrocarbons" ///
                                            6300 "Halogenated hydrocarbons" ///
                                            6301 "Bromochlorodifluoromethane" ///
                                            6302 "Chloroform" ///
                                            6303 "Tetrachloroethylene" ///
                                            6304 "Trichloroethane" ///
                                            6305 "Trichloroethylene" ///
                                            6399 "Halogenated hydrocarbons NEC" ///
                                            6900 "Other volatile solvents NEC" ///
                                            6901 "Acetone" ///
                                            6902 "Ethyl" ///
                                            6999 "Other volatile solvents NEC" ///
                                            7100 "Cannabinoids" ///
                                            7101 "Cannabinoids agonists" ///
                                            7102 "Cannabinoid" ///
                                            7199 "Cannabinoids NEC" ///
                                            9000 "MISCELLANEOUS" ///
                                            9100 "Diuretics" ///
                                            9101 "Antikaliuretics" ///
                                            9102 "Loop diuretics" ///
                                            9103 "Thiazides" ///
                                            9199 "Diuretics, NEC" ///
                                            9200 "Opioid antagonists NEC" ///
                                            9201 "Naloxone" ///
                                            9202 "Naltrexone" ///
                                            9299 "Opioid" ///
                                            9300 "MISCLLANEOUS" ///
                                            9301 "Laxatives" ///
                                            9900 "Other"
    label values pt25 Prim_drug
    Click image for larger version

Name:	Screenshot 2023-06-15 at 4.00.48 pm.png
Views:	1
Size:	24.1 KB
ID:	1717221



    However, if I used /* ... */ to disable the following part, which is the last section of the value labels (as seen below), and remove the three slashes after "Naltrexone", the code works!
    It appears that the code only runs up to the "Naltrexone" position and from "Opioid" onwards the code refuses to execute.
    Believe it or not, if you add the 9299 "Opioid" line to the code, STATA will pop up with a invalid syntax error.

    Code:
                                            ...
                                           ...
    ​​​​​​​                                        9201 "Naloxone" ///
                                            9202 "Naltrexone"
                                    /*      9299 "Opioid" ///
                                            9300 "MISCLLANEOUS" ///
                                            9301 "Laxatives" ///
                                            9900 "Other "  */
    label values pt25 Prim_drug
    I hope I can count on your help here. Fortunately, the part of the code that STATA refuses to execute is not too much and I can enter these value labels manually as well.
    Last edited by Shen YANG; 15 Jun 2023, 01:00.

  • #2
    For your testing purposes, the following variable (pt25) can be generated:
    Code:
    clear
    set obs 4
    egen pt25 = fill(1101 1201 2100 2401)

    Comment


    • #3
      You seem to be showing different versions of your code.

      Code:
       
       9202 "Naltrexone"
      needs a /// after it. I note several near duplicates such as (there are many more; the percents are meaningless here)
      Code:
                                               |      Freq.     Percent        Cum. ----------------------------------------+-----------------------------------                               "Alcohol" |          1        5.88        5.88                          "Alcohols NEC" |          1        5.88       11.76                              "Alcohols" |          1        5.88       17.65                             "Aliphatic" |          2       11.76       29.41                           "Amphetamine" |          2       11.76       41.18                      "Amphetamines NFD" |          1        5.88       47.06                          "Amphetamines" |          1        5.88       52.94      "Anabolic androgenic steroids NEC" |          1        5.88       58.82          "Anabolic androgenic steroids" |          1        5.88       64.71                          "Anaesthetics" |          2       11.76       76.47           "Atypical antipsychotics NEC" |          1        5.88       82.35               "Atypical antipsychotics" |          1        5.88       88.24                          "Barbiturates" |          2       11.76      100.00
      -

      Comment


      • #4
        Hi Nick Cox, I'm sorry I've been testing the code over and over again, which has resulted in a copy and paste error.

        I have updated the first code. Thanks for pointing out the duplicates. Would it be the cause of the issue? My understanding is that sometimes drugs with different codes (e.g., 4900 & 4999) can have the same drug name (Other anabolic agents). Sounds a bit odd, but doesn't seem to be the core of the problem?

        Comment


        • #5
          Using the updated example in #1, I cannot reproduce the problem in either Stata 17 or 18. The code runs without error.

          Comment


          • #6
            My apology, everyone. Looks like the problem is on my side.

            I agree with daniel klein , When using the value label code and the test variable, it seems the code runs without any error. However, if I run the code with real data with around 5600 observations, there's an error. I will go back and check the values in the original data. Still feels weird, but thanks for everyone's test.

            Forum admin may delete this thread if needed.

            Comment

            Working...
            X