Automatically Separating Data into Excel Sheets
Page 1 of 1
Automatically Separating Data into Excel Sheets
You can use the macro language with a PROC EXPORT to automatically generate a Excel workbook with one sheet for each level of a classification variable. In the following example we want to create a workbook (CLASS_AGES.XLS). We need to break up the data using AGE as a classification variable. Each age is to be shown on its own sheet in the workbook.
There are several ways to do this, however for this example we will create a series of macro variables; one for each level of the classification variable. This SQL step will create the macro variables &IDAGE1, &IDAG2, and so on up to 99 levels of AGE. The actual number of levels is stored in the automatic macro variable &SQLOBS.
proc sql noprint;
select distinct age
into :idage1 - :idage99
from sashelp.class;
%let agecnt = &sqlobs;
quit;
We then use a macro %DO loop to work through the list of AGE levels and create a PROC EXPORT for each, using a WHERE= data set option. Notice that the sheet name is changed for each of the values of the classification variable. &&IDAGE&I will resolve to the value of the variable AGE.
%do i = 1 %to &agecnt;
PROC EXPORT DATA = sashelp.class(where=(age=&&idage&i))
OUTFILE="C:\temp\class_ages.xls"
DBMS= excel
REPLACE;
sheet = "Age_&&idage&i";
RUN;
%END;
The full macro becomes:
%macro multisheet;
proc sql noprint;
select distinct age
into :idage1 - :idage99
from sashelp.class;
%let agecnt = &sqlobs;
quit;
%do i = 1 %to &agecnt;
PROC EXPORT DATA = sashelp.class(where=(age=&&idage&i))
OUTFILE="C:\temp\class_ages.xls"
DBMS= excel
REPLACE;
sheet = "Age_&&idage&i";
RUN;
%END;
%mend multisheet;
%multisheet
So easy.....
There are several ways to do this, however for this example we will create a series of macro variables; one for each level of the classification variable. This SQL step will create the macro variables &IDAGE1, &IDAG2, and so on up to 99 levels of AGE. The actual number of levels is stored in the automatic macro variable &SQLOBS.
proc sql noprint;
select distinct age
into :idage1 - :idage99
from sashelp.class;
%let agecnt = &sqlobs;
quit;
We then use a macro %DO loop to work through the list of AGE levels and create a PROC EXPORT for each, using a WHERE= data set option. Notice that the sheet name is changed for each of the values of the classification variable. &&IDAGE&I will resolve to the value of the variable AGE.
%do i = 1 %to &agecnt;
PROC EXPORT DATA = sashelp.class(where=(age=&&idage&i))
OUTFILE="C:\temp\class_ages.xls"
DBMS= excel
REPLACE;
sheet = "Age_&&idage&i";
RUN;
%END;
The full macro becomes:
%macro multisheet;
proc sql noprint;
select distinct age
into :idage1 - :idage99
from sashelp.class;
%let agecnt = &sqlobs;
quit;
%do i = 1 %to &agecnt;
PROC EXPORT DATA = sashelp.class(where=(age=&&idage&i))
OUTFILE="C:\temp\class_ages.xls"
DBMS= excel
REPLACE;
sheet = "Age_&&idage&i";
RUN;
%END;
%mend multisheet;
%multisheet
So easy.....
pallav- Posts : 98
Join date : 2012-03-14
Location : Ahmedabad
Similar topics
» FDA Electronic data submission requirement
» ETL processing using SAS Data Integration (DI) Studio
» SAS Programming Tip: Combining Data Sets
» SAS Programming Tip: Subsetting Data with a WHERE Statement
» Q&A: The SAS XPORT Transport Format as an FDA Standard for Data Sets
» ETL processing using SAS Data Integration (DI) Studio
» SAS Programming Tip: Combining Data Sets
» SAS Programming Tip: Subsetting Data with a WHERE Statement
» Q&A: The SAS XPORT Transport Format as an FDA Standard for Data Sets
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Mon Jun 10, 2013 2:57 pm by Admin
» SAS Enterprise Guide: Explore Data before you start Analysing
Mon Jun 10, 2013 2:53 pm by Admin
» Predictive Analytics
Fri May 31, 2013 3:30 pm by Admin
» Creating Frequency Report and Creating User Defined styles in SAS Add-In for Microsoft Office
Tue Oct 23, 2012 2:11 pm by pallav
» Prompted Reports in BI Dashboard
Thu Oct 11, 2012 4:54 pm by kushal
» Free SAS Webinar : Getting Started with SAS Hash Object
Wed Oct 10, 2012 6:08 pm by pallav
» Which SAS procedure changes the name of a permanent format for a variable stored in a SAS data set?
Tue Oct 02, 2012 12:47 am by rajin_kumar@yahoo.com
» What is written to SAS Log?
Tue Oct 02, 2012 12:39 am by rajin_kumar@yahoo.com
» SAS Portal and BI Dashboard: Customizing the Tabs
Tue Oct 02, 2012 12:33 am by kushal