DATA Step, Macro, Functions and more

.txt output with fixed columns

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

.txt output with fixed columns

I am trying to create .txt output with column names and output in columns.  I've tried Proc Export, Data _null_ with Puts, ODS (multiple versions).  The results I keep getting returned is warped text.  I need columns.

 

Here are 3 of my many attempts:

 

proc export data=sashelp.class

  outfile="/home/jm25324/class.txt"

  dbms=tab replace ;

run;

 

 

data _null_; set sashelp.class ;

  file '/home/jm25324/test.txt' dlm=",";

  If _n_=1 then do;

     put 'Name, Sex, Age, Height, Weight';

  end;

  put name sex age height weight;

run;

 

ods csvall file='/home/jm25324/test5.txt' ;

proc report data=sashelp.class nocenter headline headskip spacing=10 nowd;

column name sex age height weight;

define name / display order "Name" left width=40 ;

define sex / display "Sex" left width=40 flow;

define age / display "Age" left width=40 flow;

define height / "Height" left width=40 flow;

define weight / "Weight" left width=40 flow;

run;

ods csvall close;

 

Thanks!

 

 

 

 


Accepted Solutions
Solution
Wednesday
Occasional Contributor
Posts: 17

Re: .txt output with fixed columns

Posted in reply to Cynthia_sas
Here is the solution using the options TERMSTR=crlf:

data _null_;
set sashelp.class ;
file '/home/jm25324/test6.txt' TERMSTR=crlf;
If _n_=1 then do;
put @1 'Name' @ 20 'Sex' @25 'Age' @30 'Height' @40 'Weight';
end;
put @1 name @ 20 sex @25 age @30 height @40 weight;
run;

View solution in original post


All Replies
PROC Star
Posts: 1,405

Re: .txt output with fixed columns

Posted in reply to jeninemilum

You can use the column pointer @ like this

 

data _null_; 
  set sashelp.class ;
  file '/home/jm25324/test.txt';
  If _n_=1 then do;
     put @1 'Name' @ 20 'Sex' @25 'Age' @30 'Height' @40 'Weight';
  end;
  put @1 name @ 20 sex @25 age @30 height @40 weight;
run;
SAS Super FREQ
Posts: 9,434

Re: .txt output with fixed columns

Posted in reply to jeninemilum

Hi:

  I'm not sure of the purpose of fixed width columns with a comma separator. Do you mean something like this? I'd do it using full control of my PUT statements:

fixed_width_and_comma.png

I don't really understand what you mean when you say "with column names and output in columns" but this is how I interpreted it.

 

Cynthia

Solution
Wednesday
Occasional Contributor
Posts: 17

Re: .txt output with fixed columns

Posted in reply to Cynthia_sas
Here is the solution using the options TERMSTR=crlf:

data _null_;
set sashelp.class ;
file '/home/jm25324/test6.txt' TERMSTR=crlf;
If _n_=1 then do;
put @1 'Name' @ 20 'Sex' @25 'Age' @30 'Height' @40 'Weight';
end;
put @1 name @ 20 sex @25 age @30 height @40 weight;
run;

Super User
Posts: 13,942

Re: .txt output with fixed columns

Posted in reply to jeninemilum

Fixed column normally means something like variable 1 will occupy columns 1 through 10, variable 2 columns 11 to 17, variable 3 columns 18 to 25 for example and no specific character between values though spaces occur if the length of the variable value is smaller than the space used.

Is that what you want? You are showing comma separated values in your header row in the data _null_ data step and requesting comma separated columns in for the proc report output.

 

Perhaps you are looking for something like:

ods listing file='/home/jm25324/test5.txt';
proc print noobs data=sashelp.class;
run;
ods listing close;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 66 views
  • 0 likes
  • 4 in conversation