Announcement

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

  • How to replace specific values with string?

    Hi I am suffering from a annoying situation.


    In my data set, a variable is like below,
    Code:
    commend:
    tab v1
    
    resutls:
      02 Dec 08 |         20        0.96       68.72
      08 Dec 08 |         24        1.15       73.00
      09 Dec 08 |          1        0.05       73.05  <--------------- (a)
      15 Dec 08 |         15        0.72       77.57
      24 Dec 08 |          4        0.19       81.81
      05 Jan 09 |          9        0.43       86.04  <--------------- (b)
      06 Jan 09 |          5        0.24       86.28
      13 Jan 09 |          7        0.34       88.35
      14 Jan 09 |         10        0.48       88.84
      21 Jan 09 |          8        0.38       92.88
      30 Jan 09 |          4        0.19       93.94
      31 Jan 09 |          1        0.05       93.98
      04 Feb 09 |          7        0.34       95.43
      05 Feb 09 |          2        0.10       95.52  <--------------- (c)
    Here, I want to replace the values including "09" at the end of the value to missing value.
    What I am saying is that I need to replace the values between (b) and (c) such as 05 Jan 09 and 05 Feb 09 to missing, but I do not want to replace the value of (a), 09 Dec 08, although the (a)'s value also include "09".

    Here, how to deal with this situation?

    Thank you for your time spent to read this question.

  • #2
    Assuming v1 is a string variable, and that you want to make the entire variable string missing if it ends in "09", try this:

    Code:
    replace v1 = "" if substr(strtrim(v1), -2, 2) == "09"
    See -help string functions- to learn more in this area.

    Comment


    • #3
      Mike Lacy Sorry about my poor explanation for the data type. v1 is a long variable with format %dD_m_Y. I tried to use the way you recommended, but it does not work for my data. I think, an alternative way is needed now. Thank you for your time spent to response this question.

      Comment


      • #4
        The situation is considerably more complicated with numeric variables that represent dates. I'm not a frequent user of date variables, so all I can do here is to suggest that you do what I would have to do, which is to work through the documentation under -help date-.

        Comment

        Working...
        X