Hi Everyone,
How can we write text to an external text file and on output window while using data _null_. Please explain with example.
What makes you think that you files are all one line? The files you posted all have multiple lines.
Windows and Unix use different standards for how to mark the end of line in a text file. So if you write your files on a Unix machine the end of lines will be marked by single linefeed characters ('0A'x). If you then try to read that file using the Windows program NOTEPAD it will not recognise the single linefeeds as starting new lines since it is expecting to see the two character sequence of carriage return plus a linefeed ('0D0A'x). You can open the file using WORDPAD instead of NOTEPAD at it will look correct.
You can add the TERMSTR=CRLF option to your FILE statement and SAS will terminate each line with '0D0A'x, even when running on Unix.
Hi
To write to a file you need the FILE and PUT statement. They are used always as a pair. Find below an example.
For more details please have a look at DATA Step Processing and the doc for the FILE and PUT statements.
data _null_;
set sashelp.class;
file "c:\temp\someFile.txt";
put name age sex;
file print;
put name age sex;
run;
Bruno
Thanks Bruno,
Can you tell me to how to get output in external file in format like this (instead of getting all values in single line):
Alfred 14 M Alice 13 F Barbara 13 F Carol 14 F Henry 14 M James 12 M Jane 12 F Janet 15 F Jeffrey 13 M John 12 M Joyce 11 F Judy 14 F Louise 12 F Mary 15 F Philip 16 M Robert 12 M Ronald 15 M Thomas 11 M William 15 M
The sample code creates the file exactly as you need it, see screenshot below. Maybe you have created the file on a Unix/Linux and the editor you use to view the file can not display the data correctly.
The same code create this file on Linux
Bruno
So Can't I do anything to get output in required format i.e. every new observation on new line. My OS is Windows 10.
Show the code that you ran that failed to create a text file with one observation per record and the log.
The approach that @BrunoMueller suggests has worked on windows machines since SAS version 6 and Windows 95.
Hi Ballardw,
Below are the codes:
data _null_;
set sashelp.class;
file "/folders/myfolders/someFile.txt";
put name age sex;
file print;
put name age sex;
run;
Here is the LOG:
What do you mean by the words "add spaces/tabs"?
If you want to write a special character like a tab you can use a hexadecimal literl. '09'X would be a tab.
put age '09'x name;
If you want to write a structured file that uses a special delimiter between fields, like a CSV file, then use the DSD and DLM options on the FILE statement.
file "/folders/myfolders/someFile.txt" dsd ;
put name sex ;
would write
Alfred,M
file "/folders/myfolders/someFile.txt" dsd dlm='09'x;
put name sex ;
would write
Alfred<tab>M
I meant that only I am able to add tabs ( '07'x etc.) or spaces (" ") between two observations but I am not able to get each new observation in new line.
1. Is there any way to get new observation in new line while writing a text file (using file and put statements)? or can we use new line character line '\n' in C Programs?
2. Besides this can we print variable names as well?
Note: However, for csv files I am able to get one observation per line by using:
file "/folders/myfolders/someFile2.csv" dsd;
What makes you think that you files are all one line? The files you posted all have multiple lines.
Windows and Unix use different standards for how to mark the end of line in a text file. So if you write your files on a Unix machine the end of lines will be marked by single linefeed characters ('0A'x). If you then try to read that file using the Windows program NOTEPAD it will not recognise the single linefeeds as starting new lines since it is expecting to see the two character sequence of carriage return plus a linefeed ('0D0A'x). You can open the file using WORDPAD instead of NOTEPAD at it will look correct.
You can add the TERMSTR=CRLF option to your FILE statement and SAS will terminate each line with '0D0A'x, even when running on Unix.
Each PUT statement writes a new line, unless you add a trailing @ sign.
You use a slash in the PUT statement to begin a new line in a single put statement.
put name @ ; put age ;
is the same as
put name age ;
and
put name ; put age ;
is the same as
put name / age ;
Sorry for late reply from my side, but can you reply to second question / point of my last message viz.
2. Besides this can we print variable names as well? (while we are printing files (text or csv) by using data _null_)
Not sure I understand. Can you explain what you want? Perhaps with example output.
I mean can I get output like this:
Name Gender Age Weight *<----- Can I get variable name as well (in output file);
abc M 68 72
xyz F 62 68
The easiest way is to just use PROC EXPORT to write the file.
If you want to do it with data step then you need to add extra code to get the variable names. One way is to do it in an extra step.
proc transpose data=sashelp.class(obs=0) ;
var _all_;
run;
data _null_;
set &syslast end=eof;
file 'myfile.txt' ;
put _name_ @;
if eof then put ;
run;
Then you can add another step to write the data, just remember to use the MOD option on the FILE statement so that it appends instead of creating a new file.
data _null_;
set sashelp.class ;
file 'myfile.txt' mod ;
put (_all_) (+0) ;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.