Announcement

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

  • Dropping observations of a string variable that are not stored in a local macro

    Hello,

    I am using Stata 17/BE for Mac and I want to drop observations if certain strings are NOT observed in a string variable named cours.

    I identified the strings I wish to keep with levelsof (there are a lot of them) and I stored them in a local macro:

    levelsof cours if niv>=10 & niv!= . & c==".", local(levs)


    Now I want to drop the observations that don't contain the strings stored in local(levs) and under one condition (if c=="."). I know I can’t code cours!=levs, but I want is something like:

    drop if c=="." & cours!=levs

    I hope what I want to do is clear. Could someone please help me?

    Thank you very much,

    Nicolas Charette

    Here is a reduced sample of my data, if it can help:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int niv str25 cours byte note str4 c
      . "anglais1recycleANGL1C"      60 "ang"
      . "musique1ercycleMUSI1C"      80 "."  
      . "edu.phy1ercycleEPSA1C"      76 "."  
      . "math.1ercycleMATH1C"        74 "math"
      . "e.m.r.c.1ercycleEMRC1C"     87 "."  
      . "francais1ercycleFRAN1C"     81 "fran"
      . "a.plast.1ercycleARTP1C"     87 "."  
     40 "mathematique22400"          70 "math"
     40 "anglais2ecycle17400"        42 "ang"
     40 "francais14400"              60 "fran"
     60 "anglais17600"               72 "ang"
     60 "francais14600"              62 "fran"
     60 "mathematique22600"          67 "math"
     70 "anglais134104"              51 "ang"
     70 "mathematique63106"          60 "math"
     70 "francais132108"             36 "fran"
      . "ethiqueetculture69102"      86 "."  
      . "science/technologie55104"   81 "."  
      . "musique169104"              87 "."  
      . "geographie95103"            80 "."  
      . "mathematique63106"          84 "math"
      . "educationphysique43102"     73 "."  
      . "anglais134104"              70 "ang"
      . "histoire/education87103"    79 "."  
      . "francais132108"             72 "fran"
     80 "mathematique63206"          80 "math"
     80 "anglais134204"              69 "ang"
     80 "francais132208"             74 "fran"
     90 "francais132308"             65 "fran"
     90 "anglais134304"              60 "ang"
     90 "mathematique63306"          70 "math"
    100 "science/technologie55444"   75 "."  
    100 "educationphysique43402"     87 "."  
    100 "artsplastiques168402"       73 "."  
    100 "mathematique63414"          82 "math"
    100 "ethiqueetculture69404"      78 "."  
    100 "francais132406"             71 "fran"
    100 "football/sports46454"       96 "."  
    100 "anglais134404"              55 "ang"
    100 "histoire/education87404"    83 "."  
    110 "anglais134404"              81 "ang"
    110 "anglais(pr.local)134451"    81 "ang"
    110 "francais132506"             65 "fran"
    110 "francais(pr.local)130551"   65 "fran"
    111 "ethiqueetculture69502"      82 "."  
    111 "anglais(prog.local)134551"  78 "ang"
    111 "informatique111551"         91 "."  
    111 "anglais134504"              78 "ang"
    111 "mathematique63504"          87 "math"
    111 "artculinaire115571"        100 "."  
    end
    Last edited by Nicolas Charette; 25 Aug 2022, 09:32. Reason: I edited several times because I thought I was being unclear (I hope I am now)

  • #2
    Presuming you mean "observations in which levs contains the value in cours," I think this does what you want

    Code:
    levelsof cours if niv>=10 & niv!= . & c==".", local(levs)
    gen byte trouve =  strpos(`"`levs'"', cours) > 0
    drop if (c == ".") & !trouve
    Last edited by Mike Lacy; 25 Aug 2022, 12:03.

    Comment


    • #3
      Thank you Mike! It worked like a charm!

      Nicolas

      Comment

      Working...
      X