<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to categorize data using date variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555512#M154603</link>
    <description>&lt;P&gt;The format is defined to work on the variable with the actual date values, not the character variable GROUP.&lt;/P&gt;</description>
    <pubDate>Wed, 01 May 2019 23:40:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-05-01T23:40:32Z</dc:date>
    <item>
      <title>How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555503#M154596</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a new SAS user, I have a dataset with several variables containing patient's information&amp;nbsp; such as age, gender, clinical status, etc. I want to categorize my data into a 'before' and 'after' using a variable called 'date_current_status'. This 'date_current_status' is stored as a character variable. I first attempted to convert the variable into a SAS-date variable, then tried to use 'if then-else' statements to create the groups. However, when I tried to get the frequencies for the groups, I found that SAS has categorized all my data in one group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I used:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*CREATE NEW SAS DATE VARIABLE FROM THE DATE VARIABLES IN THE DATA*/&lt;/P&gt;&lt;P&gt;Data kano4;&lt;BR /&gt;set kano3;&lt;BR /&gt;format datevar date11.;&lt;BR /&gt;datevar = input (date_current_status, MMDDYY10.);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*check the above step*/&lt;/P&gt;&lt;P&gt;proc contents data=kano4;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/*categorize study participants into before and after*/&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data groups;&lt;/P&gt;&lt;P&gt;set kano4;&lt;/P&gt;&lt;P&gt;length Group $15;&lt;/P&gt;&lt;P&gt;Date=datepart(DATEVAR);&lt;/P&gt;&lt;P&gt;if date &amp;gt; '30-SEP-2016'D and date &amp;lt;= '30-SEP-2017'D then&lt;/P&gt;&lt;P&gt;Group="BEFORE";&lt;/P&gt;&lt;P&gt;else if date &amp;gt;= '01-OCT-2017'D and date &amp;lt;= '01-OCT-2018'D then&lt;/P&gt;&lt;P&gt;Group= "AFTER";&lt;/P&gt;&lt;P&gt;else Group="Other";&lt;/P&gt;&lt;P&gt;proc freq data= groups nlevels;&lt;/P&gt;&lt;P&gt;table group /out=Groupdate outcum nopercent;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help regarding this will be appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 22:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555503#M154596</guid>
      <dc:creator>Rahim9</dc:creator>
      <dc:date>2019-05-01T22:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555504#M154597</link>
      <description>&lt;P&gt;It is best to provide actual example date of your data set.&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without seeing actual values it is hard to tell why things didn't work as expected&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to try using a custom format. The logic is pretty simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value beforeafter
'30SEP2016'd &amp;lt;- '20SEP2017'd ='Before'
'01OCT2017'd -  '01OCT2018'd ='After'
other= 'Other';


data example;
   do date = '15AUG2016'd to '20OCT2018'd;
      output;
   end;
run;

proc freq data=example;
   tables date;
   format date beforeafter.;
run;
&lt;/PRE&gt;
&lt;P&gt;The format approach is extremely flexible when coupled with date values as if you need a different grouping all you need is a different format and to use it as needed. The format will be honored by most analysis, reporting and graphic procedures.&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 22:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555504#M154597</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-01T22:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555505#M154598</link>
      <description>&lt;P&gt;Hi and welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, did the conversion to numeric date values succeed?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, this kind of binning problem is easier handled with a format. I can't see your data so the below code is untested. Let me know if it works for you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format; 
value dtfmt   '30sep2016'd &amp;lt;- '30sep2017'd = "Before" 
              '30sep2017'd &amp;lt;- '01oct2018'd = "After" 
               other                       = 'Other'; 
run;

proc freq data= groups nlevels;
   table group /out=Groupdate outcum nopercent;
   format group dtfmt.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 May 2019 22:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555505#M154598</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-01T22:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555509#M154601</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for helping me out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After attempting to do the numeric conversion, I used proc contents procedure and found that a new variable 'datevar' was created and the format was 'DATE11' This, I suppose indicates the conversion was a success.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I used the code you provided, I get an error message with a message that "YOU ARE TRYING TO USE A NUMERIC FORMAT DTFM WITH A CHARACTER VARIABLE GROUP".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure I understand how to share my data in the forum for better communication.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 23:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555509#M154601</guid>
      <dc:creator>Rahim9</dc:creator>
      <dc:date>2019-05-01T23:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555512#M154603</link>
      <description>&lt;P&gt;The format is defined to work on the variable with the actual date values, not the character variable GROUP.&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 23:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555512#M154603</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-01T23:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to categorize data using date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555514#M154605</link>
      <description>&lt;P&gt;In your original program, this statement is incorrect and should be removed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Date=datepart(DATEVAR);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;DATEVAR is already a date variable.&amp;nbsp; The DATEPART function is appropriate when your incoming variable is a DATETIME, not a DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is possible that just removing this statement will give you the results you are seeking.&amp;nbsp; To be certain, we would need to see a few examples of the contents of DATEVAR and DATE_CURRENT_STATUS.&amp;nbsp; Or you could just remove the offending statement and see if the results are to your liking.&amp;nbsp; (Of course, the statements that follow would also need a slight adjustment.&amp;nbsp; They would have to work with DATEVAR, instead of DATE.)&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 01:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-categorize-data-using-date-variable/m-p/555514#M154605</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-02T01:56:47Z</dc:date>
    </item>
  </channel>
</rss>

