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

Hi Everyone,

 

How can we write text to an external text file and on output window while using data _null_. Please explain with example.

Regards,
AG_Stats
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

15 REPLIES 15
BrunoMueller
SAS Super FREQ

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

AG_Stats
Quartz | Level 8

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   

 

Regards,
AG_Stats
BrunoMueller
SAS Super FREQ

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.

 

Capture.PNG

 

The same code create this file on Linux

Capture.PNG

 

Bruno

AG_Stats
Quartz | Level 8

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.

Regards,
AG_Stats
ballardw
Super User

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.

AG_Stats
Quartz | Level 8

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:

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 data _null_;
57 set sashelp.class;
58 file "/folders/myfolders/someFile.txt";
59 put name age sex;
60 file print;
61 put name age sex;run;
 
NOTE: The file "/folders/myfolders/someFile.txt" is:
Filename=/folders/myfolders/someFile.txt,
Owner Name=root,Group Name=vboxsf,
Access Permission=-rwxrwx---,
Last Modified=21May2016:12:23:17
 
NOTE: 19 lines were written to file PRINT.
NOTE: 19 records were written to the file "/folders/myfolders/someFile.txt".
The minimum record length was 9.
The maximum record length was 12.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: DATA statement used (Total process time):
real time 0.17 seconds
cpu time 0.04 seconds
 
 
62
63 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
75
 
 
****************************************;
 
However, I am only able to add spaces/tabs by using the following codes:
 
data _null_;
set sashelp.class;
file "/folders/myfolders/someFile1.txt";
put name age sex " ";                       *<--- Tab is added;
file print;
put name age sex;
run;
 
Notes:
Both times by using file print I am getteing result in required format i.e. one observation per record/line.
Below are the files generated by these two codes for your reference.. Thanks.
 
 
 
Regards,
AG_Stats
Tom
Super User Tom
Super User

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

AG_Stats
Quartz | Level 8

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;

Regards,
AG_Stats
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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 ;

 

AG_Stats
Quartz | Level 8

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

Regards,
AG_Stats
Tom
Super User Tom
Super User

Not sure I understand. Can you explain what you want? Perhaps with example output.

AG_Stats
Quartz | Level 8

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

Regards,
AG_Stats
Tom
Super User Tom
Super User

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;

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
  • 15 replies
  • 45174 views
  • 11 likes
  • 4 in conversation