Announcement

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

  • Creating a new variable if another variable has a string character

    Hi All:

    I want to create a new variable that is yes/no based on whether another variable contains a string variable. Thank you in advance!

    So for example,
    newvar1 aspirin
    1 notSpecified
    0 .
    1 treatmentAsprinLT75
    1 treatmentAspirin
    1 treatmentAspirinGT200
    0
    etc.

  • #2
    I'm not sure I understand. Could you post a data extract using the dataex command?

    Here is one solution, but I don't know if this reflects your setup and what you need:

    Code:
    clear
    input byte newvar1 str30 aspirin
    1 "notSpecified"
    0 "."
    1 "treatmentAsprinLT75"
    1 "treatmentAspirin"
    1 "treatmentAspirinGT200"
    0 "."
    end
    
    gen byte newvar2 = aspirin != "."
    Code:
    . li, noobs sep(0)
    
      +-------------------------------------------+
      | newvar1                 aspirin   newvar2 |
      |-------------------------------------------|
      |       1            notSpecified         1 |
      |       0                       .         0 |
      |       1     treatmentAsprinLT75         1 |
      |       1        treatmentAspirin         1 |
      |       1   treatmentAspirinGT200         1 |
      |       0                       .         0 |
      +-------------------------------------------+

    Comment


    • #3
      Hi Kumar:
      So my data looks something like the following. I want to create a new variable called "newvar1" where the data is coded yes if there is any sort of data in "treatmentaspirinmasterwhich_base" and no if there is nothing...

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long id str59 treatmentaspirinmasterwhich_base
      255 "notSpecified"                     
      259 "treatmentAsprinLT75"                     
      260 ""                     
      261 "treatmentAspirin"                     
      262 "treatmentAspirin75200"
      end

      Comment


      • #4
        So you just need
        Code:
        label define YESNO 0 "No" 1 "Yes"
        gen byte newvar1:YESNO = (treatmentaspirinmasterwhich_base != "")

        Comment


        • #5
          Ohhhh thank you, that makes sense

          Comment

          Working...
          X