<?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: Split data set into multiple data sets based on one column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908037#M358396</link>
    <description>&lt;P&gt;I wouldn't bother to but data into macro variables.&amp;nbsp; Just use the data directly to generate the code.&lt;/P&gt;
&lt;P&gt;Do you need to make empty datasets for YEAR_MONTH values that do not exist?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then just drive it from the existing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have(keep=Year_Month)&amp;nbsp;nodupkey&amp;nbsp;out=months;
  by&amp;nbsp;Year_Month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now use the list of values to generate the code. And use %INCLUDE to run it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code ;
  put 'data' ;
  do p=1 to nobs;
    set months point=p nobs=nobs;
    put 'T_' year_month ;
  end;
  put ';'/ 'set have;' ;
  do p=1 to nobs;
    set months point=p nobs=nobs;
    put 'if ' year_month=:$quote. 'then output t_' year_month ';' ;
  end;
  put 'run;'
  stop;
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you get a program like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data 
  t_2023_01
  t_2023_02
;
set have;
if year_month="2023_01" then output t_2023_01;
if year_month="2023_02" then output t_2023_02;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are going to use macro code and generate a lot of macro variables and then use them to generate the code the it might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split(dsname,varname,prefix);
%local i n name;
proc sql noprint ;
  select distinct &amp;amp;varname into :list1- from &amp;amp;dsname;
%let n=&amp;amp;sqlobs;
quit ;

data
%do i=1 %to &amp;amp;n;
  %let name=&amp;amp;prefix._&amp;amp;&amp;amp;list&amp;amp;i;
  &amp;amp;name.
%end;
;
  set &amp;amp;dsname;
%do i=1 %to &amp;amp;n;
  %let name=&amp;amp;prefix._&amp;amp;&amp;amp;list&amp;amp;i;
  if &amp;amp;varname = "&amp;amp;&amp;amp;list&amp;amp;i" then output &amp;amp;name. ;
%end;
  stop;
run;
%mend;

%split(dsname=have,varname=year_month,prefix=T)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Dec 2023 17:13:17 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-12-14T17:13:17Z</dc:date>
    <item>
      <title>Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907968#M358372</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to split data set into multiple data sets based on values in one column.&lt;/P&gt;
&lt;P&gt;I do it in the following way.&lt;/P&gt;
&lt;P&gt;I would like to see other ways to do it please .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
format P_date ddmmyy10.;
input P_date : date9. Y;
Year_Month=CATX('_',Year(P_date),month(P_date));
cards;
01JAN2023 10
03JAN2023 20
03JAN2023 21
03JAN2023 22
31JAN2023 30
01FEB2023 15
28FEB2023 20
28FEB2023 30
28FEB2023 34
15MAR2023 40
31MAR2023 45
17MAR2023 70
;
Run;

proc sql noprint ;                                                                                                                      
select distinct  Year_Month  ,CATX('_',"T",Year_Month) as tbl
into : V_Year_Month   SEPARATED by '+' ,:V_tbl separated by '+'                                                                                                                                                                                                                     
from  have                                                                                                                             
;                                                                                                                                      
quit ;  
%put &amp;amp;V_Year_Month;
%put &amp;amp;V_tbl;

proc sql noprint;
select count(distinct Year_Month) as n into : n
from have
;
quit;
%put &amp;amp;n;


%macro Split; 
%do j=1 %to &amp;amp;n.;
%let Year_Month=%scan(&amp;amp;V_Year_Month.,&amp;amp;j.,+);
%let tbl=%scan(&amp;amp;V_tbl.,&amp;amp;j.,+);
Data &amp;amp;tbl.;
SET have(Where=(Year_Month="&amp;amp;Year_Month."));
Run;
%end;
%mend;
%split
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Dec 2023 06:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907968#M358372</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-14T06:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907977#M358376</link>
      <description>&lt;P&gt;Why do you think you need to split the data for further processing? Please show what you are doing after the split.&lt;/P&gt;
&lt;P&gt;Since statistical procedures respect formatted values, it may well be that you don't even need the additional variable.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 09:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907977#M358376</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-14T09:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907979#M358378</link>
      <description>&lt;P&gt;I would go with the &lt;A title="https://github.com/SASPAC/macroarray" href="https://github.com/SASPAC/macroarray" target="_self"&gt;macroArray&lt;/A&gt; package, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%array(ds=have, vars=Year_Month|YM, macarray=Y)

options mprint;
data
  %do_over(YM, phrase=%nrstr(V_%YM(&amp;amp;_i_.)))
;

  set have;

  select(Year_Month);
    %do_over(YM, phrase=%nrstr(
     when ("%YM(&amp;amp;_i_.)") output V_%YM(&amp;amp;_i_.);
     ))
    otherwise put "unknown Year_Month value!" _N_=;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;log:&lt;/P&gt;
&lt;PRE&gt;1    %array(ds=have, vars=Year_Month|YM, macarray=Y)
NOTE:[ARRAY] 3 macrovariables created
2
3    options mprint;
4    data
5      %do_over(YM, phrase=%nrstr(V_%YM(&amp;amp;_i_.)))
MPRINT(DO_OVER):   V_2023_1
MPRINT(DO_OVER):   V_2023_2
6    ;
7
8      set have;
9
10     select(Year_Month);
11       %do_over(YM, phrase=%nrstr(
12        when ("%YM(&amp;amp;_i_.)") output V_%YM(&amp;amp;_i_.);
13        ))
MPRINT(DO_OVER):   when ("2023_1") output V_2023_1;
MPRINT(DO_OVER):   when ("2023_2") output V_2023_2;
MPRINT(DO_OVER):   when ("2023_3") output V_2023_3;
14       otherwise put "unknown Year_Month value!" _N_=;
15     end;
16   run;

NOTE: There were 12 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.V_2023_1 has 5 observations and 3 variables.
NOTE: The data set WORK.V_2023_2 has 4 observations and 3 variables.
NOTE: The data set WORK.V_2023_3 has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&lt;/PRE&gt;
&lt;P&gt;BTW. with the data step's "select" statement and multiple "output" statements you go through input data only two times (first to get unique values, second to split data).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;all the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. To install and use macroArray package do:&lt;/P&gt;
&lt;UL dir="auto"&gt;
&lt;LI&gt;Enable the framework [first time only]:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/main/SPF/SPFinit.sas";
%include SPFinit; /* enable the framework */&lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Install the framework and the package on your machine in the folder you created:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;"; 
%installPackage(SPFinit macroArray) &lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;From now on, in your SAS session just run it like this:&lt;/LI&gt;
&lt;LI&gt;&lt;LI-CODE lang="sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;";
%include packages(SPFinit.sas);

%loadPackage(macroArray)  &lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;A title="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" href="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" target="_self" rel="nofollow noopener noreferrer"&gt;Link to details&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 08:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907979#M358378</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-12-14T08:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907991#M358382</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to split data set into multiple data sets based on values in one column.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In my opinion, this is an incredibly poor idea. You can leave everything in one data set, and then extract the part(s) you need when you don't need all of it (for example, by using WHERE), or better yet use the BY statement to perform analyses for each level of Year_Month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just because you can do something, does not mean you SHOULD do that thing.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 11:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/907991#M358382</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-14T11:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908011#M358386</link>
      <description>&lt;P&gt;That particular "year_month" variable is even clumsier than typical. That will not sort in date order because you have have values like 2023_1 and 2023_10 which means that your "data sets" won't appear in date order either.&lt;/P&gt;
&lt;P&gt;If you must use such character variables (no real proof shown that is needed at all) then at least control the conversion from numeric to character so things are consistent. This will create month portions of 01 02 ... 09 10 11 and 12 so at least these values will sort and appear in a reasonable order.&lt;/P&gt;
&lt;PRE&gt;Year_Month=CATX('_',Year(P_date), put(month(P_date),z2.) );&lt;/PRE&gt;
&lt;P&gt;Personally if I needed such values I would create a custom format to display them that way from the date. Or use one of the options of the YYMMXw. format such as YYMMD7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 14:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908011#M358386</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-14T14:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908037#M358396</link>
      <description>&lt;P&gt;I wouldn't bother to but data into macro variables.&amp;nbsp; Just use the data directly to generate the code.&lt;/P&gt;
&lt;P&gt;Do you need to make empty datasets for YEAR_MONTH values that do not exist?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then just drive it from the existing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have(keep=Year_Month)&amp;nbsp;nodupkey&amp;nbsp;out=months;
  by&amp;nbsp;Year_Month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now use the list of values to generate the code. And use %INCLUDE to run it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code ;
  put 'data' ;
  do p=1 to nobs;
    set months point=p nobs=nobs;
    put 'T_' year_month ;
  end;
  put ';'/ 'set have;' ;
  do p=1 to nobs;
    set months point=p nobs=nobs;
    put 'if ' year_month=:$quote. 'then output t_' year_month ';' ;
  end;
  put 'run;'
  stop;
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you get a program like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data 
  t_2023_01
  t_2023_02
;
set have;
if year_month="2023_01" then output t_2023_01;
if year_month="2023_02" then output t_2023_02;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are going to use macro code and generate a lot of macro variables and then use them to generate the code the it might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split(dsname,varname,prefix);
%local i n name;
proc sql noprint ;
  select distinct &amp;amp;varname into :list1- from &amp;amp;dsname;
%let n=&amp;amp;sqlobs;
quit ;

data
%do i=1 %to &amp;amp;n;
  %let name=&amp;amp;prefix._&amp;amp;&amp;amp;list&amp;amp;i;
  &amp;amp;name.
%end;
;
  set &amp;amp;dsname;
%do i=1 %to &amp;amp;n;
  %let name=&amp;amp;prefix._&amp;amp;&amp;amp;list&amp;amp;i;
  if &amp;amp;varname = "&amp;amp;&amp;amp;list&amp;amp;i" then output &amp;amp;name. ;
%end;
  stop;
run;
%mend;

%split(dsname=have,varname=year_month,prefix=T)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 17:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908037#M358396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-14T17:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908278#M358460</link>
      <description>&lt;P&gt;Let's say that this is the request of&amp;nbsp; users to work with multiple data sets(Each data set per month) instead of one dat set.&lt;/P&gt;
&lt;P&gt;It is a good question-&lt;/P&gt;
&lt;P&gt;Let's say that you want to create dats of loans.&lt;/P&gt;
&lt;P&gt;What would you preffer?&lt;/P&gt;
&lt;P&gt;Create one data set that every month adding rows&amp;nbsp; or every month create a new data set ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908278#M358460</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-15T17:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908283#M358461</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;What is YM??&lt;/P&gt;
&lt;P&gt;Can you explain about the 3 arguments?&lt;/P&gt;
&lt;P&gt;ds I see is the raw data set that I want to split&lt;/P&gt;
&lt;P&gt;Year_Month is the coolumn by which I want to split the data set.&lt;/P&gt;
&lt;P&gt;Where do you tell what will be the split data sets names?&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;ds=have, vars=Year_Month|YM, macarray=Y&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908283#M358461</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-15T17:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908284#M358462</link>
      <description>&lt;P&gt;What is the correct way to work?&lt;/P&gt;
&lt;P&gt;Let's say that in the bank there are loans data and the bank produce monthly reports based on this data.&lt;/P&gt;
&lt;P&gt;What is better ? Create one data set and every month add new rows to it? or every month create a new data set for example Loans_DEC2023?&lt;/P&gt;
&lt;P&gt;The problem of creating one data set is by my opion that it can contain many rows and be heavy&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908284#M358462</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-15T17:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908287#M358463</link>
      <description>&lt;P&gt;I agree&lt;/P&gt;
&lt;PRE class="gmail- language-sas"&gt;&lt;CODE&gt;Year_Month=CATX('_',Year(P_date),put(month(P_date),Z2.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is better than&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Year_Month=CATX('_',Year(P_date),month(P_date));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I ask-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add format to P_Date (For example&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;YYMMD7. as you offer)&amp;nbsp; than how would you split the data based on the criteria of year+month?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908287#M358463</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-15T17:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908294#M358464</link>
      <description>&lt;P&gt;Agree with others that data splitting is rarely a good idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, if each of your groups can fit into a hash-table, then hash-table splitting, as illustrated by Paul Dorfman in e.g. &lt;A href="http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf" target="_blank"&gt;http://www.lexjansen.com/nesug/nesug09/hw/HW04.pdf&lt;/A&gt; , allows this to be done in one pass of the data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format P_date ddmmyy10.;
  input P_date : date9. Y;
  Year_Month=put(P_date,yymmn6.) ;
cards;
01JAN2023 10
03JAN2023 20
03JAN2023 21
03JAN2023 22
31JAN2023 30
01FEB2023 15
28FEB2023 20
28FEB2023 30
28FEB2023 34
15MAR2023 40
31MAR2023 45
17MAR2023 70
;
run;

data _null_ ;
  if _n_=1 then do;
    declare hash h() ;
    h.definekey ("_n_") ;
    h.definedata ("p_date","Y","Year_Month") ;
    h.definedone () ;
  end;
  do _n_ = 1 by 1 until ( last.year_month) ;
    set have ;
    by year_month ;
    h.add() ;
  end ;
  h.output (dataset: cats("Want_",year_month) ) ;
  h.clear() ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Splits into:&lt;/P&gt;
&lt;PRE&gt;NOTE: The data set WORK.WANT_202301 has 5 observations and 3 variables.
NOTE: The data set WORK.WANT_202302 has 4 observations and 3 variables.
NOTE: The data set WORK.WANT_202303 has 3 observations and 3 variables.
NOTE: There were 12 observations read from the data set WORK.HAVE.
&lt;/PRE&gt;
&lt;P&gt;It's a lovely illustration of the power of the hash object.&amp;nbsp; Never before was it possible to use a single DATA step to read data and dynamically determine the names of output datasets from that step.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 18:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908294#M358464</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-12-15T18:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908398#M358478</link>
      <description>&lt;P&gt;The construction:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;Year_Month|YM&lt;/LI-CODE&gt;
&lt;P&gt;means: take variable "Year_Month", select only unique unique (the "|") values from it, and inset those values into a macro array: YM1, YM2, YM3, ..., YMn (where n is the number of unique values)&lt;/P&gt;
&lt;P&gt;the "ds=" parameter of the %array() macro indicates from which dataset you want to take data to create the macro array YM,&lt;/P&gt;
&lt;P&gt;the "macarray=Y" means except the YM1,..., YMn macro array, create also a %YM() macro which is then used in the %do_over() macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data sets names are created in the %do_over()'s "phrase=" parameter, pay attention what is printed in the log in the MPRINT blocks. You will notice that the code used in the "phrase=" is:&lt;/P&gt;
&lt;PRE&gt;when ("%YM(&amp;amp;_i_.)") output &lt;STRONG&gt;V_&lt;/STRONG&gt;%YM(&amp;amp;_i_.);&lt;/PRE&gt;
&lt;P&gt;so it calls the %YM() macro, and the result in the log shows:&lt;/P&gt;
&lt;PRE&gt;MPRINT(DO_OVER):   when ("2023_1") output &lt;STRONG&gt;V_&lt;/STRONG&gt;2023_1;
MPRINT(DO_OVER):   when ("2023_2") output &lt;STRONG&gt;V_&lt;/STRONG&gt;2023_2;
MPRINT(DO_OVER):   when ("2023_3") output &lt;STRONG&gt;V_&lt;/STRONG&gt;2023_3;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Detailed documentation on the %array() macro is &lt;A title="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#array-macro" href="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#array-macro" target="_self"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Detailed documentation on the %do_over() macro is &lt;A title="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#do-over-macro" href="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#do-over-macro" target="_self"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 20:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908398#M358478</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-12-15T20:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Split data set into multiple data sets based on one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908448#M358481</link>
      <description>&lt;P&gt;Keep it in one data set, make sure you have indexes based on the year month.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a view for current_year month or standard periods of analysis. For example, most analysis at a company I worked at used 2 years historic plus current year so we had one slice with just that data and additionally a current_month slice via views. Both allowed us to hardcode reference dates in our reporting so that the data was automatically updated without needed to redefine data in reports. I do work at a company where we store monthly files as well, but that's because each months raw data is 50 million rows of data....&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 22:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-data-set-into-multiple-data-sets-based-on-one-column/m-p/908448#M358481</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-12-15T22:56:34Z</dc:date>
    </item>
  </channel>
</rss>

