Text File Creation in DI

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Text File Creation in DI

Hi there,

 

I am trying to create a text file using user written code.

What I need to do is have a line at the top of the text file up to 100 characters.

Then I need to output my main data which could consist of a few thousand rows, which I am using the below code for.

PROC PRINT DATA=TCMPROC.TEST;
RUN;

PROC EXPORT DATA=TCMPROC.TEST
OUTFILE="\\C:\invoice_names.txt"
DBMS=TAB REPLACE;
PUTNAMES=YES;
RUN;
PROC PRINT;
RUN;

 

Then at the end of the file I need another line of data around 100 characters long to finish the text file.

 

How would I go about merging a unique header and footer record around the main data that I have by building on the code above?

 

Thanks,

Aidan 


Accepted Solutions
Solution
‎01-09-2017 04:38 AM
Contributor
Posts: 52

Re: Text File Creation in DI

[ Edited ]

Hey,

 

If i understood correctly, you want to ass header and footer to your file.

 

You can use the FILE and put statement to write the header in the file and you can use the below code to write to the file.

But you need to remove the REPLACE option, as that would delete your file(and the header too).

 

Once the file is exported through proc export, you can open the file again and using option- MOD( its opens the file and append contents to the end of the file), See an example below:

 

DATA _null_;
file out mod;
put "------your footer here ----";
run;

 

 


Cheers from India!

Manjeet

View solution in original post


All Replies
Solution
‎01-09-2017 04:38 AM
Contributor
Posts: 52

Re: Text File Creation in DI

[ Edited ]

Hey,

 

If i understood correctly, you want to ass header and footer to your file.

 

You can use the FILE and put statement to write the header in the file and you can use the below code to write to the file.

But you need to remove the REPLACE option, as that would delete your file(and the header too).

 

Once the file is exported through proc export, you can open the file again and using option- MOD( its opens the file and append contents to the end of the file), See an example below:

 

DATA _null_;
file out mod;
put "------your footer here ----";
run;

 

 


Cheers from India!

Manjeet
Contributor
Posts: 57

Re: Text File Creation in DI

Thank you I will try this today and let you know how it goes
Respected Advisor
Posts: 3,887

Re: Text File Creation in DI

@mnjtrana

I believe your code should be:

DATA _null_;
  file out mod;
  put "------your footer here ----";
  stop;
run;
Contributor
Posts: 52

Re: Text File Creation in DI

Thanks, you are right.


Cheers from India!

Manjeet
Super User
Posts: 5,256

Re: Text File Creation in DI

In some environments all files are required to have header/footers. If this is the case for you, I would invest some time in creating a User Written Transformation instead, so you caneed reuse this logic.
Data never sleeps
Respected Advisor
Posts: 3,887

Re: Text File Creation in DI

[ Edited ]

It's good practice with DIS to avoid user written code as far as possible. I totally understand that this is not always feasable - but we can at least try.

 

What you could do is to have a pre-process implemented as user written code or as user transformation if you need this more than once and only write the header, then use a normal File Writer and add 'mod' under options so that the additional output gets appended to the external file.

 

Capture.PNG

 

In my working example I'm using the following code in the User Written transformation (Code generation mode: User written body):

data _null_;
file "&_OUTPUT";
put "--- my header line here ----";
run;

And then in the File Writer:

Capture.PNG

 

Which creates the following output:

--- my header line here ----
"Alfred","M","14","69","112.5"
"Alice","F","13","56.5","84"
"Barbara","F","13","65.3","98"
.....

 

Contributor
Posts: 57

Re: Text File Creation in DI

Thank you for this, works very well with what I need.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 240 views
  • 5 likes
  • 4 in conversation