Options

[SOLVED] Using date-values in context

Mario_HofmannMario_Hofmann Member Posts: 9 Contributor II
edited November 2018 in Help
Hello,

I ve got a question regarding the usage of dates in the context. I set a macro startdate e.g. as "2012-12-01" in a macro.

When I want to calculate the difference to a date in an example set I m not able to convert the variable in a valid time string. Using date_parse_custom(%{startdate},"yyyy-MM-dd","de") does not work and gives me only errors.

I created a workaround by setting multiple macros for each year, month and day and reset the current date. There I encountered the problem that the month adds 1 month to the current date. E.g. date_set(date_now(),"12",DATE_UNIT_MONTH) returns 2013-01-014.

Thanks for any advice!

Regards,

Mario

Answers

  • Options
    MariusHelfMariusHelf RapidMiner Certified Expert, Member Posts: 1,869 Unicorn
    Hi Mario,

    nice to see that you are actually using RapidMiner!

    Please remember that the macro is replaced *before* the actual evaluation of the expression. That means that you have to surround it with quotes in this case. Please have a look at the attached process for a working example.

    Best!
    Marius
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.3.000">
      <context>
        <input/>
        <output/>
        <macros>
          <macro>
            <key>date</key>
            <value>2012-01-28</value>
          </macro>
        </macros>
      </context>
      <operator activated="true" class="process" compatibility="5.3.000" expanded="true" name="Process">
        <process expanded="true" height="145" width="279">
          <operator activated="true" class="generate_data_user_specification" compatibility="5.3.000" expanded="true" height="60" name="Generate Data by User Specification" width="90" x="112" y="30">
            <list key="attribute_values">
              <parameter key="date" value="date_parse_custom(&quot;%{date}&quot;, &quot;yyyy-MM-dd&quot;)"/>
            </list>
            <list key="set_additional_roles"/>
          </operator>
          <connect from_op="Generate Data by User Specification" from_port="output" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>
  • Options
    [Deleted User][Deleted User] Posts: 0 Learner II
    Hi Mario,

    perhaps you only missed to quote the macro in 
    date_parse_custom(%{startdate},"yyyy-MM-dd","de")
    ?

    I have attached an example process in which I used the date in the context to calculate the elapsed time between 2 dates.

    Please keep in mind that the results represents the time in ms and you have to convert it afterwards (e.g. by dividing it by 86 400 000 to get the time in days  ;) ).

    Hope that helps!

    Best,
    Edin

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.2.008">
      <context>
        <input/>
        <output/>
        <macros>
          <macro>
            <key>startdate</key>
            <value>2012-12-01</value>
          </macro>
        </macros>
      </context>
      <operator activated="true" class="process" compatibility="5.2.008" expanded="true" name="Process">
        <process expanded="true" height="161" width="681">
          <operator activated="true" class="generate_data_user_specification" compatibility="5.2.008" expanded="true" height="60" name="Generate Data by User Specification" width="90" x="45" y="30">
            <list key="attribute_values">
              <parameter key="now" value="date_now()"/>
              <parameter key="startdate" value="date_parse_custom(&quot;%{startdate}&quot;,&quot;yyy-MM-dd&quot;)"/>
            </list>
            <list key="set_additional_roles"/>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="5.2.008" expanded="true" height="76" name="Generate Attributes" width="90" x="179" y="30">
            <list key="function_descriptions">
              <parameter key="diff_to_now" value="date_diff(startdate,now)"/>
              <parameter key="diff_in_days" value="date_diff(date_parse_custom(&quot;%{startdate}&quot;,&quot;yyyy-MM-dd&quot;),date_parse_custom(date_str_custom(date_now(),&quot;yyyy-MM-dd&quot;),&quot;yyyy-MM-dd&quot;))/86400000"/>
            </list>
          </operator>
          <connect from_op="Generate Data by User Specification" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
          <connect from_op="Generate Attributes" from_port="example set output" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>
  • Options
    MariusHelfMariusHelf RapidMiner Certified Expert, Member Posts: 1,869 Unicorn
    One more remark: date_set() starts counting the months at 0, so January would be 0, February 1 etc. Additionally, the second argument should not be quoted.
  • Options
    Mario_HofmannMario_Hofmann Member Posts: 9 Contributor II
    Yes, indeed I only missed the quotes.

    Thank you!
Sign In or Register to comment.