Announcement

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

  • how to encode alphabetical variable to numerical

    Hey all,
    I have alphabetical variable that contains letters such as AA, Aa, BB, etc and I want to generate a new variable such as the following:
    gen creditrating_var= 1 if credit rating =="AA"|"Aa"|",Ba"
    how can I generate a code to accomplish this task?
    thanks for your help.

  • #2
    gen creditrating_var = creditrating=="AA" | creditrating=="Aa" | creditrating=="Ba"

    Should create a variable that equals 1 if any one of those conditions are satisfied.

    Comment


    • #3
      Thanks Ben for your responce, i used this code and it returns error:
      gen S&P_rating=2 if Creditrating=="AA+"| Creditrating=="AA"| Creditrating=="AA-"
      & invalid name
      r(198);

      Comment


      • #4
        I think it may be that you're trying to use the "&" character in your variable name. Try using only numbers (0-9), letters (a-z), and underscores(_).

        Comment


        • #5
          Thank you so much Ben, it worked well.

          Comment


          • #6
            No problem, glad it worked!

            Comment


            • #7
              HEY Ben
              I am trying to calculate the standard deviation for 14 variables ( Q1, Q2 to Q14) and I use this command:
              egen CFV_STD=sd inrange(Q1:Q14) but it is not working.
              would you by any chance know the correct code in this case?

              many thanks

              Comment


              • #8
                If you are looking to compute the standard deviation of the 14 values that occur in a single row (observation) of the dataset, then you need
                Code:
                egen CFV_STD = rowsd(Q1-Q14)
                You might also want to take a look at
                Code:
                help inrange()
                to see what that function does and the correct syntax to use it.

                Comment


                • #9
                  Thanks Hemanshu for your help.

                  Comment

                  Working...
                  X