Announcement

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

  • combining code and associated string names

    Hi all,
    Assuming you have a dataset with two columns: code (numeric) and name (string), how can you create a new variable named combined_variable that attaches the numeric code to the name?

    Best regards,
    tg

  • #2
    If you are asking about concatenating strings, see the -concat()- function of egen.

    Code:
    help egen
    You could also do it directly by adding the strings. Numerical variables need to be passed through the -string()- function.

    Code:
    egen wanted1 = concat(code name), punct(" ")
    or

    Code:
    gen wanted2 = string(code) + " " + name
    Last edited by Andrew Musau; 23 Oct 2023, 08:13.

    Comment


    • #3
      Hi Andrew,
      Thank you so much, but that is not what I want. Apologies for the confusion. When I say attach, it is not in the concat way, but when I tabulate combined_variable, Stata should show me the names whereas when I tabulate combined_variable, nolabel, Stata should show the numeric code. Hope this is clear.

      Best regards,
      tg

      Comment


      • #4
        still not completely clear to me but here's a guess - use -egen- with the group() function and the label option; see
        Code:
        h egen

        Comment


        • #5
          Apologies once again. suppose you have a sex indicator variable identifying 1=male, 2=female. So, if I type in Stata "tab sex" I will see the proportion of male and female, but if I type tab sex, nolabel I will see the proportion with numerical values 1 and 2. I want to create a variable like sex, by attaching the code (which is numeric) with its corresponding name (string).

          Comment


          • #6
            Originally posted by Tariku Getaneh View Post
            when I tabulate combined_variable, Stata should show me the names whereas when I tabulate combined_variable
            Sounds like you want to label the numerical variable "code" using values of the string variable "name". You can look at labmask from the Stata Journal.

            Code:
            net install gr0034, from(http://www.stata-journal.com/software/sj8-2)
            Code:
            labmask code, values(name)
            tab code
            tab code, nolab

            Comment


            • #7
              Thank you so much that is what I want.

              Best Wishes,
              tg

              Comment

              Working...
              X