Announcement

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

  • Creating a string variable based on another string variable.

    Hello I have the following data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 ma_thue3
    "0100106320"
    "0100104563"
    "0100104034"
    "0100106553"
    "0100686181"
    "0101147055"
    "0100106793"
    "0100102051"
    "0100101315"
    "0100104387"
    "0100774247"
    "0100102929"
    "0100109177"
    "0100100826"
    "0100109113"
    "0100101918"
    "0100828615"
    "0100108173"
    "0101506794"
    "0100110373"
    "0100108945"
    "0100100576"
    "0100101280"
    "0100100583"
    "0100103094"
    "0101473411"
    "0100114184"
    "0100100174"
    "0100105687"
    "0100103873"
    "0101444379"
    "0100102245"
    "0100100985"
    "0100105359"
    "0100107860"
    "0101508664"
    "0101440656"
    "0100110454"
    "0101452242"
    "0100111680"
    "0100103619"
    "0100100311"
    "0100100625"
    "0100100248"
    "0100106514"
    "0100107557"
    "0100104524"
    "0100106810"
    "0100106507"
    "0100108991"
    "0101486139"
    "0100103295"
    "0100102887"
    "0100104429"
    "0100102132"
    "0101487982"
    "0100100456"
    "0101526991"
    "0100816881"
    "0101516104"
    "0100104429"
    "0100104891"
    "0100108293"
    "0100100015"
    "0100101932"
    "0100100054"
    "0100100086"
    "0101011181"
    "0100103714"
    "0100102943"
    "0100110743"
    "0100109836"
    "0100111909"
    "0100111659"
    "0100111754"
    "0100110574"
    "0100103143"
    "0101066021"
    "0100109032"
    "0100150873"
    "0100687185"
    "0100106296"
    "0101483586"
    "0100100791"
    "0101049354"
    "0100107684"
    "0100102083"
    "0100779340"
    "0100100752"
    "0100102485"
    "0100101072"
    "0101405475"
    "0101375580"
    "0100107243"
    "0100100287"
    "0101517669"
    "0101493697"
    "0101619653"
    "0101495214"
    "0101493714"
    end

    I would like to create a new string variable based on this one, but with one reduced digit. So the new string variable will be the exact same except it wont contain the last digit. How can I implement this?

    Thanks,
    Jad

  • #2
    In your example data, all of the values have 10 digits. If that is true in the complete data set, then it's very easy:
    Code:
    gen wanted = substr(ma_thue3, 1, 9)
    If in the complete data set, some of the values have different numbers of digits, then it's just a little more complicated:
    Code:
    gen n_digits = strlen(ma_thue3)
    gen wanted = substr(ma_thue3, 1, n_digits-1)

    Comment


    • #3
      Thanks!

      Comment

      Working...
      X