Announcement

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

  • Generate new variable using if command

    Dear all,

    I'm trying to solve something that seems rather easy but just doesn't work out. I hope one of you can help me out:

    I'm using a dataset on cars in Belgium. Apart from sales, brands and car-characteristics, one column lists the "origin".
    For the brand "alpharomeo" the variable org = "Italy", for the brand "peugot", org="France" and so on.

    Now I want to create a variable for all cars of European origin.

    I tried the command:

    gen EU = (org == "Italy" "France" "Germany" "UK" "Sweden" "Spain" "EasternEurope")

    [The names of the countries are in red, not sure whether this is supposed to be or not.]

    but got the error: "type mismatch".


    I then found a forum that dealt with a related question and tried:

    gen newvar=""
    (298 missing values generated)

    replace newvar="EU" if org=="Italy" "France" "Germany" "UK" "Sweden" "Spain" "EasternEurope"

    But got the error: "EU" not found



    What am I doing wrong?

    Kind regards,
    Wouter

  • #2
    Perhaps you should cite the forum you tried; on this evidence it's guilty of spectacularly bad advice and should be shunned.

    See help for inlist().

    Code:
    gen newvar = "EU" if inlist(org, "Italy","France","Germany","UK", "Sweden","Spain","EasternEurope")

    Comment


    • #3
      Edit: See Nick's solution above. The code is cleaner than mine.

      Comment


      • #4
        Dear all,
        In the dataset that I worked on, there is a variable named rta, rta includes goods, goods, and services, and services. I want to create a new variable that includes only goods, and another variable that includes goods and services from rta variable. How can I do it?
        Kind regards
        Ebru

        Comment

        Working...
        X