Announcement

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

  • Formatting a String

    Hi. This will likely be a banal and rudimentary question. Apologies in advance, but I am stuck nevertheless. To my own disgrace, I am not able to troubleshoot through help format.

    I have a string variable imported from an excel file. This string variable is stored as str6 %9s. I need to format this variable such that:
    1. There are 6 characters/digits for each observation
    2. There is a decimal before the final two characters/digits, and after the first 4.

    Current values for this variable, TOP, include:

    130800
    83700
    10100
    100810

    However, I need these (and all other) values to take the following form:

    1308.00
    0867.00
    0101.00
    1008.10

    Thanks in advance.

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 var1
    "130800"
    "83700"
    "10100"
    "100810"
    end
    
    destring var1, replace
    replace var1 = var1/100
    format var1 %07.2f
    list
    Added: It dawns on me that perhaps you want the result to be a string variable, not a numeric variable which, as formatted, looks like what you want. I'm not really sure. If you do need the result to be a string, then add the following to the end of the code:
    Code:
    tostring var1, replace force usedisplayformat
    Last edited by Clyde Schechter; 05 Sep 2019, 18:47.

    Comment


    • #3
      Clyde -
      Excellent. In fact, I did need the var to be stored as a string, so thank you. Much, much appreciated.

      Comment

      Working...
      X