Announcement

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

  • Combining two variables into one decimal

    Hi all, I'm sure this is a relatively easy thing to do once you know the syntax, but essentially I have one column with a value with a number 0 through 3, and a second column with a number 0 through 99, and these represent either side of the decimal for respondent GPA. So Column A would have a 2, and Column B would have a 43, and that would represent a GPA of 2.43.

    I'm hoping to get them into one variable together, but haven't been able to figure out how yet. Thanks.

  • #2
    See if this works:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(num decimal)
    2 92
    2 85
    3 56
    3 42
    3 15
    3 63
    3 75
    end
    Code:
    gen gpa = num + (decimal / 100)
    list
    
      +----------------------+
      | num   decimal    gpa |
      |----------------------|
      |   2        92   2.92 |
      |   2        85   2.85 |
      |   3        56   3.56 |
      |   3        42   3.42 |
      |   3        15   3.15 |
      |----------------------|
      |   3        63   3.63 |
      |   3        75   3.75 |
      +----------------------+

    Comment


    • #3
      That worked, thanks! Is that easier than doing some type of concatenate command?

      Comment


      • #4
        No, it's not noticeably easier than concatenation, as that too takes one line, say

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(num decimal)
        2 92
        2 85
        3 56
        3 42
        3 15
        3 63
        3 75
        end
        
        gen wanted = string(num) + "." + string(decimal, "%02.0f") 
        
        list, sep(0) 
        
             +------------------------+
             | num   decimal   wanted |
             |------------------------|
          1. |   2        92     2.92 |
          2. |   2        85     2.85 |
          3. |   3        56     3.56 |
          4. |   3        42     3.42 |
          5. |   3        15     3.15 |
          6. |   3        63     3.63 |
          7. |   3        75     3.75 |
             +------------------------+
        The difference lies in what you want. If you want a string, the method is serious. If you want a numeric decimal, David's method is better. Incidentally, you should confirm that say 2.07 is held as 2 and 7 and can be distinguished without error from 2.70 held as 2 and 70.

        Note that you could push the string just created through real().

        Comment

        Working...
        X