<?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: Outputting data into seperate months in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537048#M33083</link>
    <description>&lt;P&gt;Again why do you need to?&amp;nbsp; It is very simple to assign a grouping variable:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;&lt;BR /&gt;  array group{12} 1;
  do i=1 to 12;&lt;BR /&gt;    if month(date) &amp;gt;= i then group{i}=1;&lt;BR /&gt;  end;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;You then have a set of group variables which you can perform procedures by, for example to run a proc means on all data from april:&lt;/P&gt;
&lt;PRE&gt;proc means data=want;
  var result;
  where group{4}=1;
  output out=april n=n;
run;&lt;/PRE&gt;
&lt;P&gt;You could of course put the grouping into one group, then do it across all by groups very simply.&lt;/P&gt;
&lt;P&gt;This code minimises the read/writes - i.e. saving processor time, and saving disk space.&amp;nbsp; Minimises the coding needed, all inbuilt simple Base SAS, and minimises upkeep.&lt;/P&gt;
&lt;P&gt;Anyways, am sure you will go ahead anyways, so here is my example - note how I put some test data in a datastep:&lt;/P&gt;
&lt;PRE&gt;data have;
  date='11JAN2019'd; result=12; output;
  date='12FEB2019'd; result=12; output;
run;

data have;
  set have;
  period=month(date);
run;

data _null_;
  do i=1 to 12;
    call execute(cats('data period_',put(i,best.),'; set have; where month(date) &amp;gt;= ',put(i,best.),'; run;'));
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will create 12 datasets, each where month of date is greater than current iteration, so 2 records in period1, 1 in period2, 0 in all others.&amp;nbsp; However do bear in mind that is 12 read/writes, + 12 header block storage, this code will run slower than doing the by group processing.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Feb 2019 12:10:11 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2019-02-20T12:10:11Z</dc:date>
    <item>
      <title>Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537021#M33079</link>
      <description>&lt;P&gt;Folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working trying to make some code as a efficient as possible. Basically I will have master dataset which I'll need to put into smaller datasets (based on months) as I receive data. So when I receive data of circa 4million records I'll need to move those records into different datasets depending what date they have on them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've created some code but I'm running into problems. See below for what I've written.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Amend the raw file you receive ;
data delivery_n ;
set data_of_interest;
time=catx('-',paydate,time_stamp);
date = input(paydate,yymmdd10.);  
format date date10.;
newdate = put(date,yymmn6.);
period='Period';
newdate1=catx('_',period,newdate);
 run;

*Write the distinct months based on variable created newdate1
proc sql noprint;
  select distinct compbl(newdate1) into :period separated by " "
  from month_t;
quit;
filename x temp;
data _null_;
  attrib period length=$6 ds length=$18;
  file x;
  i=1;
  do while(scan("&amp;amp;period",i," ") ne "");
    ds=scan("&amp;amp;period",i," ");
    period=scan(ds,-1,"_");
    put "if period=""" period +(-1) """ then output " ds ";";
    i+1;
  end;
run;

*Output your data into separate  monthly datasets 
%macro i(range_high,range_low);
	%local offset start_date end_date suffix;

	%do offset=0 %to %sysfunc(intck(month,&amp;amp;range_high,&amp;amp;range_low)) %by -1;
		%let start_date=%sysfunc(intnx(month,&amp;amp;range_high,&amp;amp;offset,b));
		%let end_date=%sysfunc(intnx(month,&amp;amp;start_date,0,e));
data &amp;amp;period ;
set month_t;
 %inc x
 if &amp;amp;start_date &amp;lt;= date &amp;lt;=&amp;amp;end_date then output i;
run;
%end;
%mend i;

%i('01dec2019'd,'01jan2019'd);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm getting the following issue;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WARNING: Source line spooling has been disabled. %INCLUDE of line numbers is not available.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 402033 observations read from the data set WORK.MONTH_T.
WARNING: The data set WORK.PERIOD_201901 may be incomplete.  When this step was stopped there were 402033 observations and 83 
         variables.
WARNING: The data set WORK.PERIOD_201902 may be incomplete.  When this step was stopped there were 402033 observations and 83 
         variables.
WARNING: The data set WORK.PERIOD_201903 may be incomplete.  When this step was stopped there were 402033 observations and 83 
         variables.
WARNING: The data set WORK.PERIOD_201904 may be incomplete.  When this step was stopped there were 402033 observations and 83 
         variables.
WARNING: The data set WORK.PERIOD_201912 may be incomplete.  When this step was stopped there were 402033 observations and 83 
         variables.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The datasets are not separating out but appear to be writing all observations to each separate dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be great.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537021#M33079</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-02-20T10:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537023#M33080</link>
      <description>I note that towards the end we should include output &amp;amp;period but the same issue occurs.</description>
      <pubDate>Wed, 20 Feb 2019 10:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537023#M33080</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-02-20T10:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537028#M33081</link>
      <description>&lt;P&gt;I can tell nothing from that log snippet other than there was an error somewhere before it.&amp;nbsp; If you really have to (and there has to be a very good reason = rare) split data up - as you can get big read/write impact on your code, it makes coding and maintenance harder etc., then something like:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have (rename=(period_start=period)) out=loop nodupkey;
  by period;
run;

data _null_;
  set loop;
  call execute(cats('data period_',period,'; set have; where period_start &amp;lt;= ',period,' &amp;lt; period_end; run;'));
run;&lt;/PRE&gt;
&lt;P&gt;Note, I do not know your data, so just made the above up as an example.&amp;nbsp; Basically this will create a distinct list of the output periods, then with this distinct list in the data null, one datastep per period is created and executed.&amp;nbsp; Its a lot simpler than macro loops.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537028#M33081</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-20T10:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537033#M33082</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to further elaborate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My dataset have as a sas data variable called (period) which ranges from 1JAN2019 to the 31DEC2019.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need to do is write some code that places observations into different datasets based on the period.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So if any observation falls between the 1JAN2019 to the 31DEC2019 it gets outputted to the January dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If any observations falls between 1APR2019 to the 30APR2019 it gets outputted to the April dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to split data up into monthly datasets as I have to create monthly aggregated datasets for individuals in my company to carry out analysis.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hence my code to begin was trying to do this, but was running into difficulties.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537033#M33082</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-02-20T10:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537048#M33083</link>
      <description>&lt;P&gt;Again why do you need to?&amp;nbsp; It is very simple to assign a grouping variable:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;&lt;BR /&gt;  array group{12} 1;
  do i=1 to 12;&lt;BR /&gt;    if month(date) &amp;gt;= i then group{i}=1;&lt;BR /&gt;  end;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;You then have a set of group variables which you can perform procedures by, for example to run a proc means on all data from april:&lt;/P&gt;
&lt;PRE&gt;proc means data=want;
  var result;
  where group{4}=1;
  output out=april n=n;
run;&lt;/PRE&gt;
&lt;P&gt;You could of course put the grouping into one group, then do it across all by groups very simply.&lt;/P&gt;
&lt;P&gt;This code minimises the read/writes - i.e. saving processor time, and saving disk space.&amp;nbsp; Minimises the coding needed, all inbuilt simple Base SAS, and minimises upkeep.&lt;/P&gt;
&lt;P&gt;Anyways, am sure you will go ahead anyways, so here is my example - note how I put some test data in a datastep:&lt;/P&gt;
&lt;PRE&gt;data have;
  date='11JAN2019'd; result=12; output;
  date='12FEB2019'd; result=12; output;
run;

data have;
  set have;
  period=month(date);
run;

data _null_;
  do i=1 to 12;
    call execute(cats('data period_',put(i,best.),'; set have; where month(date) &amp;gt;= ',put(i,best.),'; run;'));
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will create 12 datasets, each where month of date is greater than current iteration, so 2 records in period1, 1 in period2, 0 in all others.&amp;nbsp; However do bear in mind that is 12 read/writes, + 12 header block storage, this code will run slower than doing the by group processing.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 12:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537048#M33083</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-20T12:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting data into seperate months</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537078#M33084</link>
      <description>It be the same mistake that we have all made.&lt;BR /&gt;&lt;BR /&gt;%inc x&lt;BR /&gt;&lt;BR /&gt;is missing a  semicolon.</description>
      <pubDate>Wed, 20 Feb 2019 13:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Outputting-data-into-seperate-months/m-p/537078#M33084</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-20T13:58:45Z</dc:date>
    </item>
  </channel>
</rss>

