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

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 

1 ACCEPTED SOLUTION

Accepted Solutions
mnjtrana
Pyrite | Level 9

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

7 REPLIES 7
mnjtrana
Pyrite | Level 9

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
Aidan
Quartz | Level 8
Thank you I will try this today and let you know how it goes
Patrick
Opal | Level 21

@mnjtrana

I believe your code should be:

DATA _null_;
  file out mod;
  put "------your footer here ----";
  stop;
run;
mnjtrana
Pyrite | Level 9

Thanks, you are right.


Cheers from India!

Manjeet
LinusH
Tourmaline | Level 20
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
Patrick
Opal | Level 21

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"
.....

 

Aidan
Quartz | Level 8
Thank you for this, works very well with what I need.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 7 replies
  • 1071 views
  • 5 likes
  • 4 in conversation