Announcement

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

  • Gen date variable with missing values

    Hi,
    i want to loop to convert my strings data date starting by "d" into date format. But I have variables which have no data and it blocks my loop.
    Here is my loop :

    unab varlist : d*
    foreach x in `varlist'{
    gen `x'_2=date(`x',"YMD")
    order `x'_2, after(`x')
    format `x'_2 %dd_m_y
    }


    It doesn't work for variable that have 0 value (but it stops my loop...)
    For example:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte d_1 str10 d_2
    . "2020-07-17"
    . "2020-08-21"
    . "2020-07-16"
    . "2020-10-01"
    end
    If i do my loop, it will just stop after the first variable and change nothing
    I want this with a loop :

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte d_1 float d_1_2 str10 d_2 float d_2_2
    . . "2020-07-17" 22113
    . . "2020-08-21" 22148
    . . "2020-07-16" 22112
    . . "2020-10-01" 22189
    end
    format %dd_m_y d_2_2

    Thanks you !
    Last edited by Raph Selenite; 12 Oct 2020, 05:37.

  • #2
    Code:
    unab varlist : d*
    
    foreach x in `varlist' {
        gen `x'_2=date(`x',"YMD")
        order `x'_2,after(`x')
        format `x'_2, %dd_m_y
    }
    The comma in the format statement is illegal. Your code looks fine to me otherwise. It would be fine to go


    Code:
    foreach x of var d* { 
    but your version is not wrong.

    Note that an empty string variable fed to date() just results in missing dates, so I can't follow your problem report. Also, if your wildcard d* includes any numeric variables, that would be a problem.

    Last edited by Nick Cox; 12 Oct 2020, 05:52.

    Comment


    • #3
      Ok i know, i just used destring all before.... So my variables without data were not string... That was really stupid !
      Ty !

      Comment


      • #4
        Code:
        ds d*, has(type string) 
        filters out the numeric variables. The result is accessible as `r(varlist)',

        Comment


        • #5
          Ty this is really good to know !

          Comment

          Working...
          X