Announcement

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

  • #16
    Originally posted by Paris Rira View Post
    I don't like these numeric values. They always confound me. . . . All I need is to create a dummy variable=1 when pais is PT..
    I empathize. And I have good news: you can refer to the string value via its encoded value label. The expression uses the following syntax.
    Code:
    "value_label":label_name
    I illustrate its use below (see the "// <= this" in-line comment) using your dataset and the task that you want to accomplish.

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿinputÿstr2ÿPAIS

    .ÿ
    .ÿencodeÿPAIS,ÿgenerate(pais)ÿlabel(Países)

    .ÿ
    .ÿgenerateÿbyteÿis_ptÿ=ÿpaisÿ==ÿ"PT":Paísesÿ//ÿ<=ÿthis

    .ÿ
    .ÿtabulateÿis_pt,ÿmissing

    ÿÿÿÿÿÿis_ptÿ|ÿÿÿÿÿÿFreq.ÿÿÿÿÿPercentÿÿÿÿÿÿÿÿCum.
    ------------+-----------------------------------
    ÿÿÿÿÿÿÿÿÿÿ0ÿ|ÿÿÿÿÿÿÿÿÿ69ÿÿÿÿÿÿÿ98.57ÿÿÿÿÿÿÿ98.57
    ÿÿÿÿÿÿÿÿÿÿ1ÿ|ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿ1.43ÿÿÿÿÿÿ100.00
    ------------+-----------------------------------
    ÿÿÿÿÿÿTotalÿ|ÿÿÿÿÿÿÿÿÿ70ÿÿÿÿÿÿ100.00

    .ÿ
    .ÿlistÿifÿis_pt,ÿnoobs

    ÿÿ+---------------------+
    ÿÿ|ÿPAISÿÿÿpaisÿÿÿis_ptÿ|
    ÿÿ|---------------------|
    ÿÿ|ÿÿÿPTÿÿÿÿÿPTÿÿÿÿÿÿÿ1ÿ|
    ÿÿ+---------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    I wish that this trick were more prominently documented. It's very handy and I've used it a lot over the years.

    Comment


    • #17
      It is amazing. Really appreciated. You can't imagine how much time I used to spend working on this matter. Always I was thinking what if there was a code to do it once instead of wasting some hours on changing labels to the original ones one by one. You saved my day.

      Comment


      • #18
        In response to #11:
        Code:
        g foreign_aff= 0
        replace foreign_aff=1 if PAIS=="PT"
        replace foreign_aff=0 if PAIS~="PT"
        can be reduced to
        Code:
        gen foreign_aff = PAIS == "PT"

        Comment


        • #19
          g PT = PAIS=="PT"

          Comment


          • #20
            while I also use the logic used in #'s 16 and 17, note one caveat - if you have any missing values in your original variable (here "PAIS"), you will need to add a line so that you have missing values in your new variable (unless, of course, you want those missing values in the original variable translated to a 0 in the new variable); if you want missing values for the same observations in the new variable, try
            Code:
            replace PT=. if missing(PAIS)

            Comment


            • #21
              Or, to keep it a single line:
              Code:
              gen PT = (PAIS == "PT") if !missing(PAIS)

              Comment

              Working...
              X