How does one go about adding datalines to an existing text file?
I'm on SAS 9.4, SAS EG 7.1
I tried the following (copied from somewhere), but I can't adapt it to what I need.
data _null_;
file "file_path_name" mod;
input fruit :$10.;
put fruit;
datalines;
date
elderberry
fig
;
run;
I want to append semicolon separated values both to the start and to the end of the file.
As an example assume that file_path_name looks like this:
1;2;a 2;2;b
And I want to add
0;;
to the top of the file and
2;3;c
to the end of the file.
These lines should be present in code and not in another file.
How can I do this?
You cant really mod a file to add text at the beginning, you read it then write it out, something like:
data _null_; set yourdata; infile "your_file"; file "your_file"; /* Put first update */ if _n_=1 then do; put xyz; end; input; put _input_; /* Put end update */ if eof then do; put ...; end; run;
I suppose the question would be why would you need to , you can just append your data, then the reading program would sort it anyways.
Actually what I want is to export a dataset as a semicolon separated value text file, only
I then want to output the result of this as a text file.
Does this way of phrasing things make it any simpler?
Well, firstly, and most important, delimiting a file with a semicolon and then calling it a csv doesn't work. CSV stands for Comma Separated Variable file.
For your question, simplest way would be to read in the data as it stands, write (or copy from a proc import in the log) a datastep import (using infile) of the data as it stands. Then with your dataset add the extra row(s) you need to the dataset - this means you can use all the SAS formats and such like. Then once you have your final data, output this to delimited file (*.txt), using a datastep with a file command.
@Autotelic wrote:
Actually what I want is to export a dataset as a semicolon separated value text file, only
- on top of the dataset headers, I need to add two semicolon separated row of values, the first one is a row of headers, the second one is a row of values for the first header.
- the bottom of the csv has the same structure as the dataset and are datalines which I want to be able to customize in code
I then want to output the result of this as a text file.
Does this way of phrasing things make it any simpler?
It is trivial to write a delimited file using a data step. Just use the DSD= and DLM= option on the FILE statement and write the data.
file 'myfile.txt' dsd dlm=';' ;
put a b c ;
If you want to write extra information at the top then add and IF _N_=1 THEN DO;... END; block to the data step.
if _n_=1 then do;
put 'header1';
put x1 x2 x3 ;
end;
Does the "bottom" of the file include a new header row?
Please post an example dataset (in the form of a data step that we can run to create the example data) . Also include source for the information you want to put in the first two lines. Then show an example of the output you want to create from that sample data.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.