ODS EXCEL FILE="/mypoath/myfile.xlsx" options(
frozen_headers="3"
sheet_name="#byval1");
PROC TABULATE data=out;
BY byVariable;
CLASS varA varB;
TABLES varA, varB;
RUN;
ODS EXCEL CLOSE;
The code above creates an excel-file with different sheets. There is one sheet for each value of the variable byVariable. Is there a way to create an additional sheet "ALL" which contains the results for all values of the byVariable together? I mean something like "ALL" (used in the TABLES-section). I tried BY ALL byVar already (which doesn't work).
Thanks for help!
The simple answer is NO. If you want all of the data then don't use the BY statement.
ODS EXCEL FILE="/mypoath/myfile.xlsx" options(frozen_headers="3");
ODS EXCEL options(sheet_name="ALL");
PROC TABULATE data=out;
CLASS varA varB;
TABLES varA, varB;
RUN;
ODS EXCEL options(sheet_name="#byval1");
PROC TABULATE data=out;
BY byVariable;
CLASS varA varB;
TABLES varA, varB;
RUN;
ODS EXCEL CLOSE;
There is no such option.
You can:
rerun the report without BY, or
stack the data on itself modifying the by variable to be ALL -- such that it is higher than all existant by values.
data stacked / view=stacked;
set
have
have (in=stackflag)
;
if stackflag then do;
byvar = 'A0'x || 'ALL'; * A0 forces value to be 'after' other original byVar values;
end
run;
proc tabulate data=stacked;
by byvar;
…
Note: 'A0'x is a hard space ASCII character
Related
I got a problem with Hyperlink generated by SAS ODS EXCEl.
I'm using SAS9.4TM3 and EXCEL 2013.
I coded this
data lst_tie;
NUM_TIE = '2900004227803';
output;
NUM_TIE = '2900004233852';
output;
run;
data lst_tie(drop=HL);
set lst_tie;
format HL2 $500.;
HL = "http://tier-kh.cm-cic.fr/tie6_tiers/default.aspx?trt=tiesyn&banque=02297&caisse=38848&tiers="||NUM_TIE;
HL2 = '=LIEN_HYPERTEXTE("'||HL||'";"'||NUM_TIE||'")';
run;
ods excel file = "$GRPFPU/test_tiesyn.xlsx"
options (absolute_column_width="3cm,20cm,20cm");
proc report data=lst_tie
;
column NUM_TIE
HL2;
define num_tie / "Numero" style(column)={ width=100%};
define HL2 / "Tiers" style(column)={tagattr='wraptext:no' width=100%};
quit;
ods excel close;
The URL seems well encoded :
=LIEN_HYPERTEXTE("http://tier-kh.cm-cic.fr/tie6_tiers/default.aspx?trt=tiesyn&banque=02297&caisse=38848&tiers=2900004227803";"2900004227803")
without carriage return (CR).
But, on opening the XLSX file there is a CR characters just after LIEN_HYPERTEXTE (HYPERLINK in English)
XLSX Preview 1
But if I delete the CR so the hyperlink is OK.
XLSX OK
I tried several option as WIDTH_COLUMS, Wrap Option , but no way.
Thanks
ODS EXCEL is trying to make your printout pretty by inserting physical line breaks into long lines. Apparently it doesn't notice that your value is a formula instead of plain text.
Starting with SAS 9.4M4 you can add flow="tables" to the ODS statement. See this SAS Blog post
ods excel file = "$GRPFPU/test_tiesyn.xlsx"
options (absolute_column_width="3cm,20cm,20cm"
flow="tables"
)
;
For older versions of SAS, like yours, try making the column wider so it doesn't try to wrap it. Try adding width=1000% instead of width=100% to the column with the links.
define HL2 / "Tiers" style(column)={tagattr='wraptext:no' width=1000%};
To have a clickable hyperlink I add a format
``
data lst_tie;
NUM_TIE = '2900004227803';
output;
NUM_TIE = '2900004233852';
output;
run;
data lst_tie;
set lst_tie;
format HL2 $500.;
HL = "http://tier-kh.cm-cic.fr/tie6_tiers/default.aspx?trt=tiesyn&banque=02297&caisse=38848&tiers="||NUM_TIE;
run;
data one;
set lst_tie;
retain fmtname '$urltie';
rename NUM_TIE=start;
label = HL;
run;
proc format cntlin=one;
run;
ods excel file = "$GRPFPU/test_tiesyn.xlsx"
options (absolute_column_width="3cm,20cm,20cm" flow="tables");
proc report data=lst_tie
;
column NUM_TIE
;
define num_tie / "Numero" style(column)={TAGATTR='format:0' width=1.5in url=$urltie. color=cx0000FF textdecoration=underline /*tagattr='wraptext:no' width=100%*/
};
quit;
ods excel close;
``
I am quite new to SAS. I have got a file with file extension .sas7bdat which contains daily stocks prices and percentage changes. It has almost 2 million line items. I know that I can simply double click the file and open it with SAS 9.4. But, I am looking for codes which I can type in Editor and open this file. Please help me.
After I open this file, I need to export it to excel. Since it has 2 million data, I can not export everything in a single excel tab. So, What I want to do it randomly pick (say 10,000 or 20,000) data and export only this randomly picked data to excel.
My .sas7bdat file is on desktop.
Please help.
You can use surveyselect and specify the number of records you want in your subset the use proc export.
In my Example below I create a table of 10 rows and only wanted 5 row in the subset. just change the value in my macro variable from 5 to 100000
Code:
data have;
input value;
datalines;
1
2
3
4
5
6
7
8
9
10
;
run;
%let subset=5;
proc surveyselect data=have
method=srs n=&subset. out=want;
run;
Output:
value=1
value=2
value=5
value=6
value=10
Exporting:
proc export data=sashelp.class
outfile='c:\myfiles\want.csv'
dbms=csv
replace;
run;
You can also filter on the data you are exporting, dummy example below:
proc export data=want (where=(value > 100 or location='X'))
outfile='c:\myfiles\want.csv'
dbms=csv
replace;
run;
You can use ODS. This will be simpler, but generate a file which will give warning in opening first time
libname rd_data "<Your Path to dataset>"
data temp;
set rd_data.<dataset name>;
rnd = ranuni(123456);
run;
proc sort data = temp out = temp(drop=rnd);
by rnd;
run;
**** Remember this is .xls file, not .xlsx
ods html file = <xls file path - full path>;
proc print data = temp(obs=1000);
run;
ods html close;
Alternatively, you can use DDE (Dynamic Data Exchange)
First, create a library to point to the location on the file system where the data set resides. This is a pointer (in C terms) to the directory.
libname myData "<path to folder>";
From there you can use a random number and a data step to get N random values. Alternatively, PROC SURVEYSELECT can be used, but you might not have it licensed.
data temp;
set myData.<Data Set Name>;
__rnd = ranuni(1);
run;
proc sort data=temp ;
by __rnd;
run;
data toOutput;
set temp(obs=10000 drop=__rnd);
run;
The last Data Step reads in just the first 10,000 records which you randomized above.
Then you can use PROC EXPORT to export the values.
proc export data=toOutput outfile="c:\temp\output.xlsx" dbms=xlsx replace;
sheet="MyData";
run;
The great thing here is that you can create other sheets in the file with additional exports.
proc export data=toOutput outfile="c:\temp\output.xlsx" dbms=xlsx replace;
sheet="MyData2";
run;
This would allow you to create multiple samples or even export all the data across multiple sheets.
I'm trying to export one SAS table into multiple Excel worksheets based on the value of a field (parent_account). I want each worksheet to be named the same as the parent_account. I'm using the following code that I found at http://www.tek-tips.com/viewthread.cfm?qid=1335588, but I'm getting these error messages:
A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.
Argument 2 to macro function %SCAN is not a number.
%macro export_to_excel();
%local varlist idx var;
proc sql noprint;
select distinct parent_account into: varlist separated by '||'
from todays_activity;
quit;
%let idx = 1;
%do %while ( %scan(&varlist, &idx, %str(||)) ne %str() );
%let var=%scan(&varlist, &idx, %str(||));
proc export data=sashelp.class (where=(parent_account="&var"))
outfile='My file location\Report.xls'
dbms=excel;
sheet="&var";
quit;
%let idx = %eval(&idx + 1);
%end;
%mend export_to_excel;
%export_to_excel;
You could try using ODS EXCEL. Here is an example use SASHELP.CLASS dataset.
First make sure the data is sorted by your grouping variables.
proc sort data=sashelp.class out=class ;
by sex ;
run;
Set up ODS to point to your target file. Tell it to make a new sheet for each BY group.
ods excel file="&path/class.xlsx" ;
ods excel options
(sheet_interval="bygroup"
suppress_bylines="yes"
sheet_name='GENDER'
);
You also might want to turn off other output destinations.
Then print the file using PAGEBY option. And close the ODS EXCEL destination
proc print data=class noobs;
by sex ;
pageby sex ;
var _all_;
run;
ods excel close;
To test it you could try reading it back in as data. But watch out that it will create member names with embedded spaces.
options validmemname=extend;
libname xx xlsx "&path/class.xlsx";
proc copy inlib=xx outlib=work; run;
libname xx clear ;
From SAS log
NOTE: The data set WORK.GENDER has 9 observations and 5 variables.
NOTE: The data set WORK.'GENDER 2'n has 10 observations and 5 variables.
This might be helpful
%macro export_to_excel;
proc sql noprint;
select distinct parent_account into: varlist separated by '#' from todays_activity;
select count(distinct parent_account) into:n from todays_activity;
quit;
%do i=1 %to &n;
%let var= %scan(&varlist,&i,"#");
proc export data=sashelp.class (where=(parent_account="&var"))
outfile='Your file location\Report.xls'
dbms=excel;
sheet="&var";
run;
%end;
%mend export_to_excel;
I am looking if there is a macro that would export multiple datasets into separate excel worksheets within a workbook. Would be great if I could have 10 sheets per workbook.
I can do it the usual way as below, but I have more than 100 datasets to export:
PROC EXPORT DATA=HAVE;
OUTFILE= "S:\MYEXCEL.xlsx"
DBMS=EXCEL REPLACE;
SHEET="NEW_SHEET";
RUN;
Thank you!
The general concept is that you would do something like this:
%macro export_data(file=,data=,sheet=);
proc export data=&data.
outfile="&file."
dbms=excel replace;
sheet="&sheet.";
run;
%mend export_data;
Then you need to construct your export macro calls however you want. Get a dataset with one row per dataset (use dictionary.tables in SQL or sashelp.vtable in data step) and work out however you want to the logic of the sheet names and how many workbooks you need. Google data-driven macro calls for more information.
Provided you have 'SAS/Access to PC FIle formats' licensed, this little macro does it effortlessly:
%macro SASToExcel(ImportLibrary=, ExportLocation=);
ods output members = _Members;
proc datasets lib = &ImportLibrary; run; quit;
proc sql;
select count(Name) into :NumOfDatasets from _Members;
select Name into :Dataset1-:Dataset%trim(%left(&NumOfDatasets)) from _Members;
quit;
%do index = 1 %to &NumOfDatasets;
proc export data=&ImportLibrary..&&Dataset&index.
outfile="&ExportLocation"
dbms=excel replace;
sheet="&&Dataset&index";
run;
%end;
proc datasets;
delete _Members;
quit;
%mend;
%SASToExcel(ImportLibrary=raw, ExportLocation = c:\test.xlsx);
I am running bunch of tobit models on SAS. I want to transfer the output to Excel, but I want the output from all models to be in one Excel book (as opposed to creating one book for each model.) How would one do that? Many thanks.
I have the following code, but it only reports on of the results
ODS TAGSETS.EXCELXP
file='C:\Documents and Settings\Administrator\My Documents\...\Results.xls'
STYLE=minimal
OPTIONS ( Orientation = 'landscape'
FitToPage = 'yes'
Pages_FitWidth = '1'
Pages_FitHeight = '100' );
Proc qlim Data=AD.Data;
class var1;
model don =var1 var2;
endogenous don ~ truncated (lb = 1);
Run;
Proc qlim Data=AD.Data;
class var1;
model don =var1 var2 var1*var2;
endogenous don ~ truncated (lb = 1);
Run;
quit;
ods tagsets.excelxp close;
The ODS statement controls where output is written. To begin writing to a new "worksheet", use a new ODS statement with a different sheet_name.
Here is a simple example:
ods tagsets.ExcelXP file="SASHELP.CARS Analysis.xls"
path="c:\temp" style=minimal;
ods tagsets.ExcelXP options(sheet_name="RawData" embedded_titles='Yes');
title "SASHELP.CARS Listing";
proc print data=SASHELP.CARS noobs;
run;
ods tagsets.ExcelXP options(sheet_name="MakeFreq" embedded_titles='Yes');
title "SASHELP.CARS Stats";
proc freq data=SASHELP.CARS;
table make;
run;
ods tagsets.ExcelXP close;
Note the first ODS statement just defines the workbook destination and style. A separate ODS statement is used to define each work sheet.