hello
I want to create a empty text file using SAS without using data step. How can I do that
Maybe By using fopen+fclose functions wrapped in a %sysfunc, but i am not sure, if the file is created without writing to it.
%let filrf = myfile;
%let rc = %sysfunc(filename(filrf, INSERT_PATH\empty.txt));
%let fid = %sysfunc(fopen(&filrf, o));
%let rc = %sysfunc(close(&fid));
Why this restriction to not use a data step? If you've got a Unix environment and option XCMD is set then you can use the Unix touch command via a SAS X command or similar coding options.
We can use data step also .I do not have unix environment.You can provide me the solution
@shubham1 It's as simple as below with a SAS datastep.
data _null_;
file 'c:\temp\mytxt.txt';
stop;
run;
Maybe By using fopen+fclose functions wrapped in a %sysfunc, but i am not sure, if the file is created without writing to it.
%let filrf = myfile;
%let rc = %sysfunc(filename(filrf, INSERT_PATH\empty.txt));
%let fid = %sysfunc(fopen(&filrf, o));
%let rc = %sysfunc(close(&fid));
@andreas_lds wrote:
%let fid = %sysfunc(fopen(&filrf, o));
Andreas has it right. The trick is the "O" parameter on the FOpen. If you wanted something with some error checking and that deallocates everything after creation, try the below.
%LET Path = C:\Users\jbarbour\Documents\SAS\Pgm\Training\FOPEN;
%LET Filename = FOPEN_Test.txt;
%let filrf = myfile;
%** Allocate the FileRef. **;
%let rc = %sysfunc(filename(filrf, &Path\&Filename));
%if &rc ^= 0 %then
%DO;
%put NOTE: Based on FILENAME function: &=rc;
%put %sysfunc(sysmsg());
%END;
%ELSE
%DO;
%put NOTE: Allo of FileRef &filrf successful. &=RC;
%** Open/create the file and create a File ID. **;
%let fid = %sysfunc(fopen(&filrf, O));
%put NOTE: Based on FOPEN function: &=fid;
%put %sysfunc(sysmsg());
%** Close/save the file. **;
%let rc = %sysfunc(FCLOSE(&fid));
%put NOTE: Based on FCLOSE function: &=rc;
%PUT %sysfunc(sysmsg());
%END;
%** Deallocate the FileRef. **;
%let rc = %sysfunc(filename(filrf));
%put NOTE: Based on FILENAME function: &=rc;
%put %sysfunc(sysmsg());
You may or may not want to deallocate the FileRef immediately depending on what you're doing, but I include it in the above example so that you know how to do it if you need to.
Jim
> I want to create a empty text file using SAS without using data step. How can I do that
Under Windows, you can run something like this:
x 'dir .* | find "#" > myfile.txt';
tryt this SAS code in Windows.
x 'cmd /c echo.>c:\temp\sample.txt';
ods csv file='c:\temp\void.csv';
proc sql;
select name label='#' from sashelp.class(obs=0);
quit;
ods csv close;
proc export data=sashelp.class(obs=0) outfile='c:\temp\want.txt' dbms=tab replace;
putnames=no;
run;
Use the FILE statement without writing anything to it.
data _null_;
file 'c:\downloads\empty.txt' ;
run;
What is you definition of an empty XLSX?
If you just want a zero byte file the extension on the file does not matter.
I don't think Excel would be happy with a workbook that does not have any sheets. Would you want a single sheet in the workbook? What content should the sheet have? I doubt you could make one that doesn't at least have something in at least on cell in the sheet.
Just a empty excel file with Sheet1 and have nothing,.
Just like you create a new excel file by right clicking mouse -> new create -> Microsoft Excel Sheet
I post my solution here firstly for someone who need it.
ods excel file='c:\temp\x.xlsx' options(sheet_name='Sheet1') style=journal ;
proc sql;
select name label='#' from sashelp.class(obs=0);
quit;
ods excel close;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.