<?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 How do I create a new variable based on a defined date range? in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638593#M8998</link>
    <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a new categorical variable CropYear from a predetermined range of dates in my ShipDate variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2016 - 08/09/2016 through 08/01/2017&lt;/P&gt;&lt;P&gt;2017 - 08/02/2017 through 07/29/2018&lt;/P&gt;&lt;P&gt;2018 - 07/30/2018 through 07/28/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using SAS Studio, any coding help anyone can provide would be helpful and appreciated! I'm new to SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Garrett&lt;/P&gt;</description>
    <pubDate>Thu, 09 Apr 2020 06:08:42 GMT</pubDate>
    <dc:creator>gtjoeckel</dc:creator>
    <dc:date>2020-04-09T06:08:42Z</dc:date>
    <item>
      <title>How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638593#M8998</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking to create a new categorical variable CropYear from a predetermined range of dates in my ShipDate variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2016 - 08/09/2016 through 08/01/2017&lt;/P&gt;&lt;P&gt;2017 - 08/02/2017 through 07/29/2018&lt;/P&gt;&lt;P&gt;2018 - 07/30/2018 through 07/28/2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using SAS Studio, any coding help anyone can provide would be helpful and appreciated! I'm new to SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Garrett&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 06:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638593#M8998</guid>
      <dc:creator>gtjoeckel</dc:creator>
      <dc:date>2020-04-09T06:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638596#M8999</link>
      <description>&lt;P&gt;Providing data in usable form is recommended and explaining the rules that have to be applied is essential to understand what you want. So please help us to help you.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 06:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638596#M8999</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-04-09T06:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638597#M9000</link>
      <description>&lt;P&gt;How do you calculate the ranges, or do you have them in a lookup dataset?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 06:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638597#M9000</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-09T06:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638608#M9001</link>
      <description>&lt;P&gt;I am not sure of the requirement, but i assume you have a data as have and you need a want dataset as below, please provide more details if this does not meet your requirement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input shipdate :&amp;amp;$200.;
cards;
08/09/2016 through 08/01/2017
08/02/2017 through 07/29/2018
07/30/2018 through 07/28/2019
;

data want;
set have;
cropyear=scan(scan(shipdate,1,' '),3,'/');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Apr 2020 07:30:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638608#M9001</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-09T07:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638679#M9002</link>
      <description>&lt;P&gt;BEST really is to provide an example of your actual existing data as you are not providing details that are likely very critical. Such as is your shipdate variable a Date value, character or something else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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 &amp;lt;&amp;gt; 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;One of the more flexible ways, especially since the boundary values are not "nice" (as if first day of the same month) could be a custom format &lt;STRONG&gt;if your date is a SAS date value.&lt;/STRONG&gt; The example below creates a format and a small example date set and a new character variable with the cropyear value. If your date is a SAS date value then you likely do not need to create a new variable just use the format with your existing variable. The only way to provide literal values for dates is the one shown with a quoted Date9. or Date7 format appearing value followed by D to tell SAS you want the date literal value to be used.&lt;/P&gt;
&lt;PRE&gt;Proc format library=work;
value cropyear
'09Aug2016'd - '01Aug2017'd = '2016'
'01Aug2017'd - '29Jul2018'd = '2017'
'30Jul2018'd - '28Jul2019'd = '2018'
;
run;

data example;
   do date= '09Aug2016'd, '10Sep2016'd,'01Aug2017'd, '02Aug2018'd;
      cyear = put(date,cropyear.);
      output;
   end;
   format date mmddyy10.;
run;

proc print data=example;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Apr 2020 14:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638679#M9002</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-09T14:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a new variable based on a defined date range?</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638779#M9003</link>
      <description>&lt;P&gt;Thanks all, sorry I didn't include sufficient data and code to make it clearer.&amp;nbsp; WIth ballardw post I was able to piece it together with the following code and make it work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc format library=work;&lt;BR /&gt;value cropyear&lt;BR /&gt;'09Aug2016'd - '01Aug2017'd = '2016'&lt;BR /&gt;'01Aug2017'd - '29Jul2018'd = '2017'&lt;BR /&gt;'30Jul2018'd - '28Jul2019'd = '2018';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data capstone.galanewdata;&lt;BR /&gt;set capstone.galanewdata;&lt;BR /&gt;CropYear = put(ShipDate,cropyear.);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=work.test out=work.yearsort;&lt;BR /&gt;by CropYear descending ShipDate;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=work.yearsort nodupkey;&lt;BR /&gt;by CropYear;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's last entry for each crop year:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="gtjoeckel_0-1586459651341.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38114iBF57969DEB5A1B6B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="gtjoeckel_0-1586459651341.png" alt="gtjoeckel_0-1586459651341.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Garrett&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 19:14:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-do-I-create-a-new-variable-based-on-a-defined-date-range/m-p/638779#M9003</guid>
      <dc:creator>gtjoeckel</dc:creator>
      <dc:date>2020-04-09T19:14:30Z</dc:date>
    </item>
  </channel>
</rss>

