<?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: Splitting up dataset by variables and exporting to multiple excel workbooks in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344529#M22801</link>
    <description>&lt;P&gt;Excellent! Works like a charm!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Mar 2017 07:36:51 GMT</pubDate>
    <dc:creator>cat2</dc:creator>
    <dc:date>2017-03-27T07:36:51Z</dc:date>
    <item>
      <title>Splitting up dataset by variables and exporting to multiple excel workbooks</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/343966#M22762</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table of multiple variables that I wish to split up to export to different excel workbooks.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Period&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Country&lt;/TD&gt;&lt;TD&gt;City&lt;/TD&gt;&lt;TD&gt;Miles&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201701&lt;/TD&gt;&lt;TD&gt;Peter&lt;/TD&gt;&lt;TD&gt;England&lt;/TD&gt;&lt;TD&gt;London&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201701&lt;/TD&gt;&lt;TD&gt;Peter&lt;/TD&gt;&lt;TD&gt;England&lt;/TD&gt;&lt;TD&gt;Birmingham&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201701&lt;/TD&gt;&lt;TD&gt;Francois&lt;/TD&gt;&lt;TD&gt;France&lt;/TD&gt;&lt;TD&gt;Paris&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201701&lt;/TD&gt;&lt;TD&gt;Francois&lt;/TD&gt;&lt;TD&gt;France&lt;/TD&gt;&lt;TD&gt;Marseille&lt;/TD&gt;&lt;TD&gt;55&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201701&lt;/TD&gt;&lt;TD&gt;Edouard&lt;/TD&gt;&lt;TD&gt;France&lt;/TD&gt;&lt;TD&gt;Lyon&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each unique combination of Period/Name/Country I wish to make a table to export to excel, that will be named&lt;/P&gt;&lt;P&gt;"Period - Name - Country".xlsx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried doing it with a macro, but haven't really gotten anywhere.&lt;/P&gt;&lt;P&gt;Help is much appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2017 06:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/343966#M22762</guid>
      <dc:creator>cat2</dc:creator>
      <dc:date>2017-03-24T06:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting up dataset by variables and exporting to multiple excel workbooks</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/343975#M22765</link>
      <description>&lt;P&gt;If you use a textual format, you can do it in one data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Period $ Name $ Country $ City $ Miles;
cards;
201701 Peter England London 45
201701 Peter England Birmingham 60
201701 Francois France Paris 15
201701 Francois France Marseille 55
201701 Edouard France Lyon 90
;
run;

proc sort data=have;
by period name country;
run;

data _null_;
set have;
length filnam $ 100;
filnam = '$HOME/sascommunity/' !! trim(period) !! '_' !! trim(name) !! '_' !! trim(country) !! '.csv';
file out filevar=filnam dlm=',';
by period name country;
if first.country
then do;
  put "period,name,country,miles";
end;
put
  period
  name
  country
  miles
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Mar 2017 07:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/343975#M22765</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-24T07:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting up dataset by variables and exporting to multiple excel workbooks</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344015#M22770</link>
      <description>&lt;P&gt;make a macro and call execute();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input Period	(Name	Country	  City) (: $20.)	Miles;
cards;
201701	Peter	England	London	45
201701	Peter	England	Birmingham	60
201701	Francois	France	Paris	15
201701	Francois	France	Marseille	55
201701	Edouard	France	Lyon	90
;
run;


%macro split(Period=, Name=, Country=);
proc export data=have(where=(period=&amp;amp;period and name="&amp;amp;name" and country="&amp;amp;country")) 
outfile="/folders/myfolders/&amp;amp;period-&amp;amp;name-&amp;amp;country..xlsx"
dbms=xlsx replace;
run;
%mend;

proc sql;
create table key as
 select distinct period,name,country from have;
quit;
data _null_;
 set key;
 call execute(catt('%split(period=',period,',name=',name,',country=',country,')'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Mar 2017 10:30:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344015#M22770</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-24T10:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting up dataset by variables and exporting to multiple excel workbooks</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344072#M22775</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Creating workbooks using meta data period, name, and  country and fact data city, and miles&lt;BR /&gt;&lt;BR /&gt;If a workbook creation fails the code below will pop up a window that&lt;BR /&gt;will allow the op to decide what he wants to do.&lt;BR /&gt;&lt;BR /&gt;If compilation of 'mete data' fails the program will issue a message to the log and stop;&lt;BR /&gt;&lt;BR /&gt;All in one address space.

inspired by
https://goo.gl/GiOwZx
https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/343966

SOAPBOX ON

 Really do not understand the love of 'proc import/export' and
 call execute.

 If you cannot use libname consider installing R/Python and
 on you local power workstation and us R/Python instread of EG SAS.

SOAPBOX OFF&lt;BR /&gt;  Really do not understand the love of 'proc import/export' and call execute on this list.&lt;BR /&gt;SOAPBOX OFF

HAVE
====

Up to 40 obs WORK.HAVE total obs=5

Obs    PERIOD    NAME        COUNTRY    CITY          MILES

 1     201701    Peter       England    London          45
 2     201701    Peter       England    Birmingham      60
 3     201701    Francois    France     Paris           15
 4     201701    Francois    France     Marseille       55
 5     201701    Edouard     France     Lyon            90

WANT Three workbooks using meta data
(libname creates an sheet name and a named range)
==================================================

d:/xls/wkb201701-Edouard-France.xlsx
d:/xls/wkb201701-Francois-France.xlsx
d:/xls/wkb201701-Peter-England.xlsx

Here is one of these;

d:/xls/wkb201701-Francois-France.xlsx

  +---------------------------------------------------+------------+
  |     A      |     B      |    C       |    D       |    E       |
  ----------------------------------------------------+------------+
1 |            |            |            |            |            |
  +------------+------------+------------+------------+------------+
2 |   PERIOD   |  NAME      |  COUNTRY   |  CITY      |   MILES    |
3 |   201701   |  Francois  |   France   |  Paris     |    15      |
4 |   201701   |  Francois  |   France   |  Marseille |    55      |
  +------------+------------+------------+------------+------------+

[wkb201701]


WORKING CODE
============

    Compilation and error checking DOSUBL to extract meta data into macro arrays

    Data step to iterate through meta data

    Execution and error checking DOSUBL to create excel sheets

    Pop up Window to ask op to continute if iteration fails

FULL SOLUTION
=============

*                _                  _       _
 _ __ ___   __ _| | _____        __| | __ _| |_ __ _
| '_ ` _ \ / _` | |/ / _ \_____ / _` |/ _` | __/ _` |
| | | | | | (_| |   &amp;lt;  __/_____| (_| | (_| | || (_| |
|_| |_| |_|\__,_|_|\_\___|      \__,_|\__,_|\__\__,_|

;


data have;
infile cards expandtabs truncover;
input Period (Name Country City) (: $20.) Miles;
cards4;
201701 Peter England London 45
201701 Peter England Birmingham 60
201701 Francois France Paris 15
201701 Francois France Marseille 55
201701 Edouard France Lyon 90
;;;;
run;

* just in case you rerun;
%utlfkil(d:/xls/wkb201701-Edouard-France.xlsx);
%utlfkil(d:/xls/wkb201701-Francois-France.xlsx);
%utlfkil(d:/xls/wkb201701-Peter-England.xlsx);

*          _       _   _
 ___  ___ | |_   _| |_(_) ___  _ __
/ __|/ _ \| | | | | __| |/ _ \| '_ \
\__ \ (_) | | |_| | |_| | (_) | | | |
|___/\___/|_|\__,_|\__|_|\___/|_| |_|

;

data _null_;

   if _n_=0 then do;
     %let rc=%sysfunc(dosubl('
       proc sql noprint;
          select
              max(put(period,6. -l))
             ,quote(trim(max(name)))
             ,quote(trim(max(country)))
          into
             :period  separated by ","
            ,:name    separated by ","
            ,:country separated by ","
          from
            have
          group
            by period, name, country
       ;quit;
     '));
   end;

   * check to see if compilation eas successful;
   %sysfunc(ifc(&amp;amp;rc,%str(put "Compilatiion failed";stop;),%str( )));

   array pers[&amp;amp;sqlobs]  (&amp;amp;period);
   array nams[&amp;amp;sqlobs] $20 (&amp;amp;name);
   array cnys[&amp;amp;sqlobs] $20 (&amp;amp;country);

   do i=1 to &amp;amp;sqlobs;
      call symputx('per',put(pers[i],6.));
      call symputx('nam',nams[i]);
      call symputx('cny',cnys[i]);

      rc=dosubl('
         libname xel "d:/xls/wkb&amp;amp;per.-&amp;amp;nam.-&amp;amp;cny..xlsx";
         data xel.wkb&amp;amp;per;
            set have(where=(period=&amp;amp;per and name="&amp;amp;nam" and country="&amp;amp;cny"));
         run;quit;
         libname xel clear;
      ');

     * check if each workbook was created sucessfully;
     if rc ne 0 then do;
        * popup asking if you want to cintinue;
        putlog "workbook wkb&amp;amp;per &amp;amp;nam &amp;amp;cny failed";
        window dsn irow=1 rows=12 color=white
          #3 @10
          "Continue Y/N:" +1 YN $1.;
        display dsn;
        end;
        if yn="N" then stop;

    end;

run;quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Mar 2017 14:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344072#M22775</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-24T14:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting up dataset by variables and exporting to multiple excel workbooks</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344529#M22801</link>
      <description>&lt;P&gt;Excellent! Works like a charm!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2017 07:36:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Splitting-up-dataset-by-variables-and-exporting-to-multiple/m-p/344529#M22801</guid>
      <dc:creator>cat2</dc:creator>
      <dc:date>2017-03-27T07:36:51Z</dc:date>
    </item>
  </channel>
</rss>

