Inserting a table name into a string with SAS - sas

I'm writing a macro in sas, that exports a file. I want the name of the file, to
be the same as the name of the table in sas. So if i run:
%to_excel(my_table);
I want the file to be saved to "Q:/my_table.xlsx". Heres what I have so far:
%macro to_excel(tb);
proc export data=&tb
outfile=?????????????????
dbms = xlsx
replace;
run;
%mend;

You are almost there. Try this.
%macro to_excel(tb);
proc export data=&tb
outfile="Q:/&tb..xlsx"
dbms = xlsx
replace;
run;
%mend;
%to_excel(my_table);

Related

How can I include a timestamp in a .xpt filename?

I have a SAS script that outputs a SAS .xpt file. I currently use the PROC COPY method of generating this because the required name includes dashes and is longer than eight characters (which I understand is the name limit when using xport).
My code is roughly as follows:
LIBNAME TempSrc "C:\Temp";
LIBNAME xportout xport 'C:\Temp\1234-AB-FileOut_Name_.xpt';
PROC IMPORT datafile="C:\Temp\FileIn.csv"
out=mydata
dbms=dlm replace;
DELIMITER= ",";
getnames=yes;
options ExtendObsCounter=no;
RUN;
DATA TempSrc.SasFile;
set work.mydata
RUN;
PROC COPY in=TempSrc out=xportout memtype=data;
select stdy7673;
RUN;
I have recently been required to include a timestamp in the output file name.
I have these macros to generate the date and time as required:
%let today=%sysfunc(date(), date9.);
%let now=%sysfunc(time(), time5.);
%let now=%sysfunc(compress(&now, :));
I have not been able to incorporate into the LIBNAME with any success, though.
Neither of the following has worked:
LIBNAME xportout xport 'C:\Temp\1234-AB-File_Name_&today.&now..xpt';
LIBNAME xportout xport 'C:\Temp\1234-AB-File_Name_' || &today. || &now. '.xpt';
How can I include the datetime in the .xpt filename?
Macro variables won't resolve in 'single quotes'. Use "double quotes" as follows:
LIBNAME xportout xport "C:\Temp\1234-AB-File_Name_&today.&now..xpt";

export datasets into multiple sheets of one excel file in sas

I'm use this code
proc export data=goldsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="gold";
run;
proc export data=platinumsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="platinum";
run;
proc export data=titaniumsheet_invalid outfile="C:\Documents and Settings\sasadm\Desktop\gold.xls" dbms=xls replace;
sheet="titanium";
run;
Error:Statement is not valid or it is used out of proper order
Note:- already try dbms=xlsx or dbms=EXCELCS but not work
Instead of using a PROC EXPORT this can be accomplished with older versions of SAS using ODS (Output Delivery System) statements. Going this route is not as clean as the PROC EXPORT but if all you want is to get the data from these data sets to a single Excel workbook and have the results of each proc statement on a different worksheet this will do it.
In this case the code to accomplish what you are looking for would be:
ods tagsets.excelxp file='C:\temp\gold.xml' options(sheet_name = 'Gold' sheet_interval='proc');
proc print data=goldsheet_invalid;
run;
ods tagsets.excelxp options(sheet_name = 'Platinum');
proc print data=platinumsheet_invalid;
run;
ods tagsets.excelxp options(sheet_name = 'Titanium');
proc print data=titaniumsheet_invalid;
run;
ods tagsets.excelxp close;
You will notice that the file extension created is XML, this is a necessity. When you load the file in Excel is would appear as expected and feel free to update the file extension from there.
More details about SAS and ODS can be found at: https://support.sas.com/rnd/base/ods/odsmarkup/TipSheet_ods_xl_xp.pdf

PROC EXPORT outfile row 2

I'm trying to export the column names of a sas data to a xlsx file but need the data to be copied starting in the 2nd row of the excel file. What I have right now:
PROC EXPORT DATA= mylib.test
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
sheet = "test1";
range = "test1$A2:BE2000";
run;
However, I get an error indicating that the RANGE statement is not supported and is ignored in Export procedure
Any suggestions?
Try the data set option FIRSTOBS.
PROC EXPORT DATA= mylib.test (firstobs=2)
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
run;
Edit: If by"starting in the 2nd row" you mean to output the data without the variable names, then you have to use PUTNAMES=NO;
PROC EXPORT DATA= mylib.test
outfile = "exceltobemodified.xlsx"
dbms = excel replace;
PUTNAMES=NO;
run;
Load your table with a blank row as first row. Try writing the table to excel file then. It should work.
Proc sql
insert into test
values('',.,'')
quit;
Proc sort data=test;
by _all_;
run;
Options missing='';
proc export data=test outfile='/home/libname/new.xlsx'
dbms=excel replace;
putnames=no;
run;

How can I create SAS html files with names corresponding to a dataset field

I'm trying to create a file for each record in my SAS dataset. I have been able to succesfully do this using SAS ODS output by using the "newfile=page" option and then simply running a proc report on the dataset. The problem I'm having is that this results in gerneric, sequentially numbered file names (test, test1, test2, etc...). I'd like to name each file based on a variable in the dataset. In the example below, I'd like the file names to be titled based on the "seq_nbr" in the dataset.
ods html path = "C:\test\"
file = 'test.html'
contents = 'contents.html
frame = 'frame.html'
code = (url="C:\test\javascript.js")
newfile=page;
proc report data = test2 nowindows headline headskip;
column tDate tTime created_by cmtText;
by seq_nbr;
run;
You need a macro for this
%macro doit;
proc sql;
select count(*) into :n from test2 ;
quit;
%do i=1 %to &n;
data _null_;
if _n_=&i call symput('name',seq_nbr);
run;
proc export data=something outfile="&name";
run;
%end;
%mend;
%doit;

How can I assign a value to a column as I'm using PROC EXPORT?

%if %sysfunc(exist(working.__extra_nos__)) %then %do;
proc export data=working.__extra_nos__
dbms=oracle replace;
password="&password.";
tablename="sch.no_selection_&env_type.";
url="&dburl.";
username="&user.";
run;
sch.no_selection_&env_type also has column called identifier, which isn't in __extra_nos__ so I want to set it to &identifier as I export it.
How can I do this?
It's a lot easier to access database DBMSs by LIBNAME rather than PROC EXPORT.
http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a003113591.htm
libname mydblib oracle user=testuser password=testpass path=hrdept_002;
[adjust for your oracle installation details]
Then instead of export, you just create or modify a table using normal language (SQL or Data Step)...
proc sql;
create table mydblib.sch.no_selection_&env_type. as
select *, "&identifier" as identifier from work.tempextras;
quit;
or
data mydblib.sch.no_selection_&env_type.;
set work.tempextras;
identifier="&identifier";
run;
Create a dataset from your __extra_nos__ and put the identifier in then. Then export that dataset.
data work.tempextras;
set working.__extra_nos__;
identifier = &identifier.;
run;
%if %sysfunc(exist(working.__extra_nos__)) %then %do;
proc export data=work.tempextras;
dbms=oracle replace;
password="&password.";
tablename="sch.no_selection_&env_type.";
url="&dburl.";
username="&user.";
run;
proc datasets library = work; /*delete the temp dataset*/
delete tempextras;
run;