BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shubham1
Calcite | Level 5

hello

 

I want to create a empty text file using SAS without using data step. How can I do that 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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));

 

View solution in original post

14 REPLIES 14
Patrick
Opal | Level 21

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.

shubham1
Calcite | Level 5

We can use data step also .I do not have unix environment.You can provide me the solution 

Patrick
Opal | Level 21

@shubham1 It's as simple as below with a SAS datastep.

data _null_;
  file 'c:\temp\mytxt.txt';
  stop;
run;
andreas_lds
Jade | Level 19

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));

 

jimbarbour
Meteorite | Level 14

@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

ChrisNZ
Tourmaline | Level 20

> 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';

 

 

japelin
Rhodochrosite | Level 12

 

tryt this SAS code in Windows.

x 'cmd /c echo.>c:\temp\sample.txt';
Ksharp
Super User
ods csv file='c:\temp\void.csv';
proc sql;
select name label='#' from sashelp.class(obs=0);
quit;
ods csv close;
Ksharp
Super User
proc export data=sashelp.class(obs=0) outfile='c:\temp\want.txt' dbms=tab replace;
putnames=no;
run;
Tom
Super User Tom
Super User

Use the FILE statement without writing anything to it.

data _null_;
  file 'c:\downloads\empty.txt' ;
run;
Ksharp
Super User
Tom,
Great, Could you create an empty XLSX file ?
Tom
Super User Tom
Super User

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.

Ksharp
Super User

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

 

Ksharp_0-1627728563339.png

 

Ksharp
Super User

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;

 

 

 

 

Ksharp_0-1627728748846.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 2801 views
  • 10 likes
  • 8 in conversation