Announcement

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

  • Finding values by label names (Tab if label name contains XY)

    Hi there,

    I have a long list of people being situated in different regions. Those NUTS3 Regions are labelled e.g. by "DE111 Stuttgart" (DE being the country, DE1 being the NUTS 1 Region, DE11 being the NUTS2 Region). In the dataset they are coded by numerical values from 1 to 942 and unfortunately alphabetically arranged.
    I want to create a new variable "nuts1". Thus, I thought I could use a command like:

    recode nuts3 ("DE1**"=1) ("DE2**"=2), gen(nuts1)


    where DE1** stands for all label values starting with DE1.
    However, I'm aware that searching for labels instead of values makes it more complicated.

    Hope anyone can help


  • #2
    recode is about mapping from numeric to numeric. You can make some progress to your goal by applying decode to get a string variable out of the value labels, and then e.g. conditioning on a substring. That can be more work than just using the underlying numeric values.

    Comment


    • #3
      elabel (SSC or SJ) might be helpful. For example,

      Code:
      elabel list (nuts3) iff strmatch(@, "DE1*")
      returns in r(values) the respective numeric values.

      However, I would probably go with decode as Nick suggested. Even if it were more cumbersome than looking up numeric values, I guess it would make for better readable code.

      Comment


      • #4
        Thanks for the help. In case anyone else reads this post in 20 years, here is what I did:
        1. Step: decode nuts3, gen(nuts2)
        2. Step: replace nuts2 = "DE1" if strpos( nuts2, "DE1")

        Comment


        • #5
          How far does

          Code:
          substr(nuts2, 1, 3)
          get you?

          Comment

          Working...
          X