SAS export subset of column to worksheet with a column name
Given a SAS dataset with columns named n1,n2,..nN
.
Is there a simple way to export common set of columns and unique subset of columns to a workbook, where each column is exported to the worksheet with the same name as the last column name?
Example:
For the SAS dataset above, the columns:
n1, n2, n5
-> Worksheetn5
n1, n2, n9
-> Worksheetn9
n1, n2, n13
-> Worksheetsn13
are exported to a Excel workbook, with worksheets named as above.
Appreciate any suggestions.
excel sas export
add a comment |
Given a SAS dataset with columns named n1,n2,..nN
.
Is there a simple way to export common set of columns and unique subset of columns to a workbook, where each column is exported to the worksheet with the same name as the last column name?
Example:
For the SAS dataset above, the columns:
n1, n2, n5
-> Worksheetn5
n1, n2, n9
-> Worksheetn9
n1, n2, n13
-> Worksheetsn13
are exported to a Excel workbook, with worksheets named as above.
Appreciate any suggestions.
excel sas export
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48
add a comment |
Given a SAS dataset with columns named n1,n2,..nN
.
Is there a simple way to export common set of columns and unique subset of columns to a workbook, where each column is exported to the worksheet with the same name as the last column name?
Example:
For the SAS dataset above, the columns:
n1, n2, n5
-> Worksheetn5
n1, n2, n9
-> Worksheetn9
n1, n2, n13
-> Worksheetsn13
are exported to a Excel workbook, with worksheets named as above.
Appreciate any suggestions.
excel sas export
Given a SAS dataset with columns named n1,n2,..nN
.
Is there a simple way to export common set of columns and unique subset of columns to a workbook, where each column is exported to the worksheet with the same name as the last column name?
Example:
For the SAS dataset above, the columns:
n1, n2, n5
-> Worksheetn5
n1, n2, n9
-> Worksheetn9
n1, n2, n13
-> Worksheetsn13
are exported to a Excel workbook, with worksheets named as above.
Appreciate any suggestions.
excel sas export
excel sas export
edited Nov 11 '18 at 22:18
Hedgehog
asked Nov 11 '18 at 22:09
HedgehogHedgehog
3,42722732
3,42722732
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48
add a comment |
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48
add a comment |
2 Answers
2
active
oldest
votes
Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:tempsandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:tempsandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
add a comment |
I guess, What I understood from your question is how to have sheet name with last variable of SAS dataset. One way to do this is to use dictionary.columns and find which column position (varnum in dictionary.columns)is max in a dataset which will give the last variable and you can make a macro variable out of this and use this for sheet in proc export.
/* pick up the last variable*/
proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and varnum = (select max(varnum) from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS");
/* use the macrovariable in your sheet statement*/
PROC EXPORT DATA= Sashelp.Class /*Sheet 1*/
outfile= "/folders/myfolders/class.xlsx "
dbms=xlsx replace;
sheet="&mysheet";
run;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253745%2fsas-export-subset-of-column-to-worksheet-with-a-column-name%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:tempsandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:tempsandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
add a comment |
Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:tempsandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:tempsandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
add a comment |
Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:tempsandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:tempsandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..
Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:tempsandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:tempsandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..
edited Nov 11 '18 at 23:28
answered Nov 11 '18 at 23:13
RichardRichard
8,96721227
8,96721227
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
add a comment |
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
Very nice. The macro I came up with was more clunky. Much appreciated.
– Hedgehog
Nov 12 '18 at 2:54
add a comment |
I guess, What I understood from your question is how to have sheet name with last variable of SAS dataset. One way to do this is to use dictionary.columns and find which column position (varnum in dictionary.columns)is max in a dataset which will give the last variable and you can make a macro variable out of this and use this for sheet in proc export.
/* pick up the last variable*/
proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and varnum = (select max(varnum) from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS");
/* use the macrovariable in your sheet statement*/
PROC EXPORT DATA= Sashelp.Class /*Sheet 1*/
outfile= "/folders/myfolders/class.xlsx "
dbms=xlsx replace;
sheet="&mysheet";
run;
add a comment |
I guess, What I understood from your question is how to have sheet name with last variable of SAS dataset. One way to do this is to use dictionary.columns and find which column position (varnum in dictionary.columns)is max in a dataset which will give the last variable and you can make a macro variable out of this and use this for sheet in proc export.
/* pick up the last variable*/
proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and varnum = (select max(varnum) from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS");
/* use the macrovariable in your sheet statement*/
PROC EXPORT DATA= Sashelp.Class /*Sheet 1*/
outfile= "/folders/myfolders/class.xlsx "
dbms=xlsx replace;
sheet="&mysheet";
run;
add a comment |
I guess, What I understood from your question is how to have sheet name with last variable of SAS dataset. One way to do this is to use dictionary.columns and find which column position (varnum in dictionary.columns)is max in a dataset which will give the last variable and you can make a macro variable out of this and use this for sheet in proc export.
/* pick up the last variable*/
proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and varnum = (select max(varnum) from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS");
/* use the macrovariable in your sheet statement*/
PROC EXPORT DATA= Sashelp.Class /*Sheet 1*/
outfile= "/folders/myfolders/class.xlsx "
dbms=xlsx replace;
sheet="&mysheet";
run;
I guess, What I understood from your question is how to have sheet name with last variable of SAS dataset. One way to do this is to use dictionary.columns and find which column position (varnum in dictionary.columns)is max in a dataset which will give the last variable and you can make a macro variable out of this and use this for sheet in proc export.
/* pick up the last variable*/
proc sql ;
select name into :mysheet TRIMMED from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS"
and varnum = (select max(varnum) from dictionary.columns
where libname = "SASHELP"
and memname = "CLASS");
/* use the macrovariable in your sheet statement*/
PROC EXPORT DATA= Sashelp.Class /*Sheet 1*/
outfile= "/folders/myfolders/class.xlsx "
dbms=xlsx replace;
sheet="&mysheet";
run;
edited Nov 12 '18 at 3:05
answered Nov 12 '18 at 2:56
Kiran Kiran
2,8153919
2,8153919
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253745%2fsas-export-subset-of-column-to-worksheet-with-a-column-name%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If those last columns are all the same type, character or numeric, you can transpose the data and have this be entirely data driven using ODS EXCEL and BY GROUPS. Similar to this: support.sas.com/rnd/base/ods/odsmarkup/excelxp_demo.html#data
– Reeza
Nov 12 '18 at 5:48