<?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: Create output datasets on-the-fly by a variable (Where I may not know the different values) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402538#M278847</link>
    <description>&lt;P&gt;BY group processing within ODS EXCELXP or ODS EXCEL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data" target="_blank"&gt;https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2017 23:51:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-10-09T23:51:24Z</dc:date>
    <item>
      <title>Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402529#M278844</link>
      <description>&lt;P&gt;If I have a dataset with a variable called "Color"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From this dataset, I would like to create separate datasets based on color&lt;/P&gt;&lt;P&gt;(but I might not always know how many different colors are in the dataset) - meaning, I don't want to have to hardcode a list of all possible colors rather I want to sort the file by color, then as the color changes, I want to create a new dataset and name it the new the new /next value of the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So if I had a dataset with records containing the "color"&amp;nbsp; Red. Blue. Green.&amp;nbsp; - I would want to output 3 datasets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DSN1 = Color_Blue&lt;/P&gt;&lt;P&gt;DSN2&amp;nbsp;= Color_Green&lt;/P&gt;&lt;P&gt;DSN3 = Color_Red&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there were more colors, I want to output a dataset for each based on their "Color"&lt;/P&gt;&lt;P&gt;DSN.n - Color_&amp;lt;var-value&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 22:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402529#M278844</guid>
      <dc:creator>thockada</dc:creator>
      <dc:date>2017-10-09T22:11:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402533#M278845</link>
      <description>&lt;P&gt;You could write a macro where you cycle through a unique list of the colors in the data set (untested).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table color_list as
select distinct color
from your_data;
quit;

%macro output_color_sets;

proc sql;
select color into :color1-:color99 from color_list;
quit;

%do i = 1 %to &amp;amp;sqlObs;


data color_&amp;amp;&amp;amp;color&amp;amp;i;
set your_data;
where color = "&amp;amp;&amp;amp;color&amp;amp;i";
run;

%end;


%mend output_color_sets;
%output_color_sets
&lt;/CODE&gt;&lt;/PRE&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;This might do what you want without a macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://analytics.ncsu.edu/sesug/2007/SD04.pdf" target="_blank"&gt;http://analytics.ncsu.edu/sesug/2007/SD04.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 22:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402533#M278845</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-10-09T22:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402536#M278846</link>
      <description>&lt;P&gt;Awesome, thanks for the quick response - I will try these out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One twist - what if I wanted to create a single excel&amp;nbsp; spreadsheet of the same with each color as a different tab/sheet?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 22:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402536#M278846</guid>
      <dc:creator>thockada</dc:creator>
      <dc:date>2017-10-09T22:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402538#M278847</link>
      <description>&lt;P&gt;BY group processing within ODS EXCELXP or ODS EXCEL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data" target="_blank"&gt;https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 23:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402538#M278847</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-09T23:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402574#M278848</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/170592"&gt;@thockada&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Or as a variant code which can output tables to any destination:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length color $20;
  do color='Blue', 'Red', 'Green';
    do i=1 to 10;
      output;
    end;
  end;
  stop;
run;

proc sort data=have;
  by color;
run;


libname mywb xlsx 'c:\temp\demo_wb.xlsx';
data _null_;
  if _n_=1 then 
    do;
      dcl hash h1(dataset:'have(obs=0)', multidata:'y');
      h1.defineKey('color');
      h1.defineData(all:'y');
      h1.defineDone();
    end;

  do until(last.color);
    set have;
    by color;
    h1.add();
  end;

  h1.output(dataset:cats('mywb.want_',color));
  h1.clear();
run;
libname mywb clear;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Oct 2017 02:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402574#M278848</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-10T02:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402591#M278849</link>
      <description>&lt;P&gt;All the solutions you've been provided so far require reading the dataset twice.&amp;nbsp; The solution below needs to read it only once, followed by renaming dummy dataset names to names based on the values encountered. The example use the sashelp.stocks data set, and it doesn't need a sorted data set, nor does it need to have enough memory to hold data temporarily in hash objects:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1 d2 d3 d4 d5 d6 d7 d8;
  length st_list $500  proc_ds $3000;
  retain st_list ' ' 
         proc_ds 'proc datasets library=work nolist; change ';

  set sashelp.stocks  end=eos;

  d=findw(st_list,trim(stock),' ','E');
  if d=0 then do;
    d=countw(st_list)+1;
    st_list=catx(' ',st_list,stock);
    proc_ds=catx(' ',proc_ds,cats('d',d,'=',stock));
  end;

  select (d);
    when (1) output d1;
    when (2) output d2;
    when (3) output d3;
    when (4) output d4;
    when (5) output d5;
    when (6) output d6;
    when (7) output d7;
    when (8) output d8;
  end;

  if eos then do;
    proc_ds=cats(proc_ds,'; delete d0');
    if countw(st_list)&amp;lt;8 then do d=countw(st_list)+1 to 8;
      proc_ds=catx(' ',proc_ds,cats('d',d));
    end;
    proc_ds=catx(' ',proc_ds,';quit;');
    call execute(trim(proc_ds));
  end;
  drop d st_list proc_ds;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This program works by creating datasets D1 through D8 (although D4-D8 have 0 observations in this example).&lt;/LI&gt;
&lt;LI&gt;In a subsequent proc datasets (see the call execute(trim(proc_ds)) statement), D1 through D3 are renamed to ibm, intel, and Microsoft.&amp;nbsp;&amp;nbsp; Data sets d4-d8 are deleted.&lt;/LI&gt;
&lt;LI&gt;Important:&amp;nbsp; Make sure the DATA statement declares enough data sets to accommodate the largest expected number of unique values (of STOCK) in the data set.&lt;BR /&gt;&lt;BR /&gt;But you can protect yourself against this by having a "miscellaneous" dataset to capture all&amp;nbsp; observations that are not output to D1-D8.&amp;nbsp; That (relatively small) miscellaneous data set&amp;nbsp;can then be submitted to the same processing as the original&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The benefits of this approach are&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;you read the dataset only once&lt;/LI&gt;
&lt;LI&gt;you don't need to know in advance what values of stock will be found (as per your original request)&lt;/LI&gt;
&lt;LI&gt;the &lt;EM&gt;&lt;STRONG&gt;dataset does not need to be sorted&lt;/STRONG&gt;&lt;/EM&gt; by stock&lt;/LI&gt;
&lt;LI&gt;you don't need to maintain a hash object, which means
&lt;OL&gt;
&lt;LI&gt;memory is conserved&lt;/LI&gt;
&lt;LI&gt;output observations are automatically&amp;nbsp;kept in the same order as incoming observations.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;bb&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2017 04:14:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402591#M278849</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-10T04:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402623#M278850</link>
      <description>&lt;P&gt;Still another method, using call execute to create a dynamic data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have (keep=color) out=lookup nodupkey;
by color;
run;

data _null_;
call execute('data');
do until (eof1);
  set lookup end=eof1;
  call execute(' ' !! trim(color));
end;
call execute('; set have; select (color); ');
do until (eof2);
  set lookup end=eof2;
  call execute('when ("' !! trim(color) !! '") output ' !! trim(color) !! '; ');
end;
call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will require two passes through your dataset, but be completely flexible; the only limit is the number of distinct color values, which could break the data statement created by the call execute if it exceeds 32767 characters. Or if it exceeds the system limit for concurrently open filehandles.&lt;/P&gt;
&lt;P&gt;A requirement is of course that the values in color can be used as valid SAS names.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2017 07:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402623#M278850</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-10T07:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create output datasets on-the-fly by a variable (Where I may not know the different values)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402858#M278851</link>
      <description>&lt;P&gt;Awesome, exactly what I needed.&amp;nbsp; Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2017 17:10:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-output-datasets-on-the-fly-by-a-variable-Where-I-may-not/m-p/402858#M278851</guid>
      <dc:creator>thockada</dc:creator>
      <dc:date>2017-10-10T17:10:16Z</dc:date>
    </item>
  </channel>
</rss>

