Announcement

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

  • Replacing Contents of All Variables

    I have a data set in which I have students' grades for their 30 subjects aprox. These are letter grades like A*, A, B,... I want to convert them into their equivalent numerical values like A*= 95, A=90, B=80, and so on. Can someone guide me how can I do that using a single command instead of using the following command for every variable and grade?

    Code:
    replace Accounting ="96" if Accounting == "A*"
    Secondly, do I also need to convert the type of the variable from string to integer?

  • #2
    Abdul:
    welcome to this forum.
    You may want to try something along the following lines:
    Code:
    . set obs 3
    number of observations (_N) was 0, now 3
    
    . g English="A"
    
    . g Maths="B"
    
    . foreach var of varlist English-Maths {
      2.                 replace `var'="1" if `var'=="A"
      3.                 replace `var'="2" if `var'=="B"
      4.         }
    (3 real changes made)
    (0 real changes made)
    (0 real changes made)
    (3 real changes made)
    
    
    . destring English Maths, replace
    English: all characters numeric; replaced as byte
    Maths: all characters numeric; replaced as byte
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      I would apply encode. You do need to define value labels ahead of time.


      Code:
      clear
      input str2 (foo bar)  
      "A*"  "A"
      "A"   "B"
      end
      
      label def numbers 95 "A*" 90 "A" 80 "B"
      
      foreach v in foo bar {
          encode `v', gen(`v'_n) label(numbers)
      }  
      
      list
      
      list, nola
      Results:

      Code:
       
      . list
      
      +---------------------------+
      | foo bar foo_n bar_n |
      |---------------------------|
      1. | A* A A* A |
      2. | A B A B |
      +---------------------------+
      
      .
      . list, nola
      
      +---------------------------+
      | foo bar foo_n bar_n |
      |---------------------------|
      1. | A* A 95 90 |
      2. | A B 90 80 |
      +---------------------------+

      If such variables really are all your variables, then try

      Code:
      foreach v of var *  {
          encode `v', gen(`v'_n) label(numbers)
      }

      Comment


      • #4
        Nick Cox seems to have been sabotaged by the forum software when pasting his output into the code block. For clarity, the output is
        Code:
        . list
        
             +---------------------------+
             | foo   bar   foo_n   bar_n |
             |---------------------------|
          1. |  A*     A      A*       A |
          2. |   A     B       A       B |
             +---------------------------+
        
        . 
        . list, nolabel
        
             +---------------------------+
             | foo   bar   foo_n   bar_n |
             |---------------------------|
          1. |  A*     A      95      90 |
          2. |   A     B      90      80 |
             +---------------------------+

        Comment


        • #5
          Thank you for help

          Comment

          Working...
          X