Announcement

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

  • How to generate string variable from numeric variable

    I have a dataset with a variable named activity_id with each unique observation having values that range from 1/7: How can I easily generate a string variable named activity_code without the repetitive replace as follows?

    g activity_code=""
    replace activity_code="a" if activity_id==1
    replace activity_code="b" if activity_id==2
    replace activity_code="c" if activity_id==3
    replace activity_code="d" if activity_id==4
    replace activity_code="e" if activity_id==5
    replace activity_code="f" if activity_id==6
    replace activity_code="g" if activity_id==7


  • #2
    if you have labelled these, you can use -decode-; if you have not labelled them you should do so as that is probably what you actually want rather than a string variable (just a guess as you don't say why you want this to be a string variable); see
    Code:
    help decode
    help label (look for the section on value labels)

    Comment


    • #3
      Welcome to the Stata Forum / Statalist,


      Since there is not data shared, this is just a general advice : you may - decode - the variable, create a label, than - encode - with the label() option.
      Best regards,

      Marcos

      Comment


      • #4
        Thanks Rich and Marcos.

        Comment


        • #5
          Good advice given, but FWIW your code example boils down to

          Code:
          g activity_code = word("a b c d e f g", activity_id)

          Comment


          • #6
            The command shared in #5 by Nick was quite new to me. I mean, the use of - word - as a function.

            There is an example (example 2) in the Stata Manual, but it uses the string variable "name" and selects a parcel of the sentence. Here, it goes far beyond, since it dovetails a given set of letters to a given set of numbers of a different variable.

            I'm glad to become aware of this possibiity!
            Best regards,

            Marcos

            Comment


            • #7
              Consider also the possibilities implied by


              Code:
              . di "`c(alpha)'"
              a b c d e f g h i j k l m n o p q r s t u v w x y z
              
              . di "`c(ALPHA)'"
              A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
              
              . cret li
              
              <stuff omitted> 
              
                             c(alpha) = "a b c d e f g h i.."
                             c(ALPHA) = "A B C D E F G H I.."
                              c(Mons) = "Jan Feb Mar Apr M.."
                            c(Months) = "January February .."
                             c(Wdays) = "Sun Mon Tue Wed T.."
                          c(Weekdays) = "Sunday Monday Tue.."

              Comment


              • #8
                Thanks Nick

                Comment

                Working...
                X