How to create a text file and show each obs in one row?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 106
Accepted Solution

How to create a text file and show each obs in one row?

 

Hi,

I'm working with a program to create a text file. It works fine but it doesn't go to the next line for the next obs and it continues with

the same line. How can I create a text file that in each row shows one record(obs)?

 

Here is my code:

data _null_ ; 
set table.abc;
FILE '/opt/sas94/home/user1/text1.txt' dsd dlm=',' ; 
PUT var1 var2 var3 var4 var5 var6 var7 var8;
run ;

 

Thanks


Accepted Solutions
Solution
‎10-24-2016 09:56 AM
Super User
Super User
Posts: 7,720

Re: How to create a text file and show each obs in one row?

You shouldn't put a | between them.  The line is terminated by a given line terminator - so long as the person importing the file knows the line terminator, they can read it in correctly.  See this article for the process:
http://support.sas.com/kb/14/178.html

 

And for more information on this topic:

https://en.wikipedia.org/wiki/Newline

View solution in original post


All Replies
Super User
Super User
Posts: 7,720

Re: How to create a text file and show each obs in one row?

What are you opening the file in to view the data?  The reason is I run this on my SAS:

data _null_; 
  set sashelp.class; 
  file 'c:\test.csv' dsd dlm=','; 
  put name sex age; 
run ;

And it works fine when I open it in Notepad.  Do note the consistent code formatting, indetation and the use of code editor {i} above your post.  Note also that I call the output file .csv, this is because you are creating a Comma Separated Variable file, so make sure the file extension describes the data correctly.


What I suspect is your opening that data in either A) a package that doesn't interpret line feed/break characters, or B) your moving it across to Windows from Unix - this is most likely and common.  Windows and Unix have different CR/LF special characters which indicate the end of line.

Frequent Contributor
Posts: 106

Re: How to create a text file and show each obs in one row?

Thank you RW9 for your reply.

I'm opening the text file in Notepad. I've corrected my code by using pipe '|' as delimeter. This file should be sent as text file:

data _null_ ; 
set table.abc;
FILE '/opt/sas94/home/user1/text1.txt' dsd dlm='|' ; 
PUT var1 var2 var3 var4 var5 var6 var7 var8;
run ;

B) your moving it across to Windows from Unix - this is most likely and common.

Yes,maybe it is the issue. There are two ways  for me to run the code:

  • Localy from my PC and connect to the server and save text file there.
  • From Citrix (Windows server) and save file on server.

In both cases I faced the same problem but it seems that it is OK for my colleague to receive the file in this way.

But I have another question. Here is an example of text file:

 

 

100000095|abcdefghijk|kj7764bbid|20JAN2015|21FEB2015|201501100000098|abcdefncvbjk|abcdehg564d5|09JAN2015|02APR2015|201501100000096

In 201501100000098, 201501 is the last value for the first record and 100000098 is the first value for the second record.

How should I change the code so '|' to be placed between 201501 and100000098?

 

Solution
‎10-24-2016 09:56 AM
Super User
Super User
Posts: 7,720

Re: How to create a text file and show each obs in one row?

You shouldn't put a | between them.  The line is terminated by a given line terminator - so long as the person importing the file knows the line terminator, they can read it in correctly.  See this article for the process:
http://support.sas.com/kb/14/178.html

 

And for more information on this topic:

https://en.wikipedia.org/wiki/Newline

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 376 views
  • 0 likes
  • 2 in conversation