Announcement

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

  • Dropping string observations starting with a number

    Hi. I have the following data and want to delete all observations where the Product starts with a number. The Product variable is a string variable. I’d appreciate any help. Thanks.

    Code:
    "1 ANTI-INFECTIVES"       
    "1 ANTI-INFECTIVES"       
    "1 ANTI-INFECTIVES"       
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "105 MISCELLANEOUS AGENTS"
    "115 NUTRITIONAL PRODUCTS"
    "115 NUTRITIONAL PRODUCTS"
    "122 RESPIRATORY AGENTS"  
    "122 RESPIRATORY AGENTS"  
    "122 RESPIRATORY AGENTS"  
    "122 RESPIRATORY AGENTS"  
    "20 ANTINEOPLASTICS"      
    "20 ANTINEOPLASTICS" 
    "ACCOLATE"
    "ABILITY"
    "ACCU-CHECK"

  • #2
    try the following:
    Code:
    drop if real(substr(product,1,1))<.
    next time please use -dataex- for your data example (see the FAQ)

    Comment


    • #3
      Example:
      Code:
      sysuse auto
      
      replace make = "1 AMC Spirit" in 3
      replace make = "5 Chev. Impala" in 15
      replace make = "0 Merc. Bobcat" in 29
      replace make = "9 Dodge Diplomat" in 21
      
      list make if ustrregexm(make, "(^[0-9])")
      drop if ustrregexm(make, "(^[0-9])")
      Result:
      Code:
      . list make if ustrregexm(make, "(^[0-9])")
      
           +------------------+
           | make             |
           |------------------|
        3. | 1 AMC Spirit     |
       15. | 5 Chev. Impala   |
       21. | 9 Dodge Diplomat |
       29. | 0 Merc. Bobcat   |
           +------------------+
      
      . drop if ustrregexm(make, "(^[0-9])")
      (4 observations deleted)

      Comment


      • #4
        Thank you. Both of those worked perfectly!

        Comment

        Working...
        X