Announcement

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

  • Variable with negative values

    Hey,

    I'm stuck on a simple thing and would really need some help. I have a non-numeric variable ranging from -5 to +5. I want to make it into only positive values so that -5 represents 1 and -4 represents 2 etc. Does anyone know how to do this easily? I suppose I first encode it but when I have done so and then recoded the variables to positive ones i only get values between 6 to 10 even though I recoded -5 to 1.

    /Johan

  • #2
    Code:
    . clear
    
    . set obs 11
    number of observations (_N) was 0, now 11
    
    . range whatever -5 5
    
    . gen wanted = whatever + 6
    
    . list, sep(0)
    
         +-------------------+
         | whatever   wanted |
         |-------------------|
      1. |       -5        1 |
      2. |       -4        2 |
      3. |       -3        3 |
      4. |       -2        4 |
      5. |       -1        5 |
      6. |        0        6 |
      7. |        1        7 |
      8. |        2        8 |
      9. |        3        9 |
     10. |        4       10 |
     11. |        5       11 |
         +-------------------+

    Comment


    • #3
      In post #2 Nick Cox overlooked your hint that your input variable is stored as a string.

      He also overlooked your reference to using encode to convert the string to a numeric variable. That was a mistake on your part. The encode command is designed for assigning numerical codes to non-numeric strings like "France", "Germany", "United States". The output of help encode instructs us

      Do not use encode if varname contains numbers that merely happen to be stored as strings; instead, use generate newvar = real(varname) or destring; see real() or [D] destring.
      In the example below, I start with your numbers from -5 to +5 stored in a string variable, use destring to create a numeric variable, then apply Nick's approach to transform them from [-5,+5] to [0,11].
      Code:
      . destring x_string, generate(x_number)
      x_string: all characters numeric; x_number generated as byte
      
      . generate wanted = x_number+6
      
      . list, clean noobs
      
          x_string   x_number   wanted  
                -5         -5        1  
                -4         -4        2  
                -3         -3        3  
                -2         -2        4  
                -1         -1        5  
                 0          0        6  
                +1          1        7  
                +2          2        8  
                +3          3        9  
                +4          4       10  
                +5          5       11  
      
      . describe *
      
                    storage   display    value
      variable name   type    format     label      variable label
      ------------------------------------------------------------------------------------------------
      x_string        str4    %9s                   
      x_number        byte    %10.0g                
      wanted          float   %9.0g                 
      
      .

      Comment


      • #4
        #3 Good point and note also


        Code:
        gen wanted = real(x_string) + 6

        Comment

        Working...
        X