Announcement

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

  • Stata Code

    Hey Stata Community,


    I'm looking for a way to generate a variable depending on the "4 last letters" of another variable.


    Example:

    My variable has car names like "VW" or "AUDI", but sometimes the variable also says "VW FGDT" (4 random letters behind the car name, no model specifications).

    How can I generate a new variable that is "1" if there is this random 4 letter code behind the car name? Is there something like a "placeholder" I can use for these random 4 letters?

    Best regards,
    Milena

  • #2
    Milena:
    do you mean something along the following lines?
    Code:
    . set obs 2
    number of observations (_N) was 1, now 2
    
    . g car_name="AUDI" in 1
    
    . replace car_name="VW FGDT" in 2
    variable car_name was str4 now str7
    
    
    . split car_name
    variables created as string:
    car_name1  car_name2
    
    . g wanted=1 if car_name1!="" & car_name2!=""
    
    . replace wanted=0 if car_name1!="" & car_name2==""
    
    . list
    
        +-----------------------------------------+
         | car_name   car_na~1   car_na~2   wanted |
         |-----------------------------------------|
      1. |     AUDI       AUDI                   0 |
      2. |  VW FGDT         VW       FGDT        1 |
         +-----------------------------------------+
    
    .
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X