BookmarkSubscribeRSS Feed
PRAVIN_JAIN
Fluorite | Level 6

I want to retain leading/trailing spaces while exporting data into a flat file, using below code its getting trimmed currently.

data _null_;
set test_data end=eof;
file "<location>/test_data.dat" dlm='01'x DSD encoding="utf-8" bom=no;
retain out_rec_count 0;
out_rec_count + 1;
put mmn rec_seq;
if eof then do;
call symputx('out_record_count', out_rec_count);
stop;
end;
run;

input dataset ::
mmn|rec_seq
 |1
MarY |2
 John |3
QUIIN |4
 1234 john |5

current output file :
^A1
MarY^A2
John^A3

QUIIN^A4
1234 john^A5


expected output file :
 ^A1
MarY ^A2
 John ^A3
QUIIN ^A4
 1234 john ^A5

 

I tried using $char50. in put statement, but it adds addition spaces to keep 50 length. Please suggest solution.

4 REPLIES 4
ballardw
Super User

You will want to repost your examples, input and output both, into a TEXT box on the forum. Open a text box by clicking on the </> icon that appears above the main message window and paste the text. The software of the forum mean that the main message windows will reformat pasted text and replace white space characters which means that we will not see what you want if the examples are pasted into the main message window.

Tom
Super User Tom
Super User

Please show the SAS code you used to write the file that did not work.  Note that using the DSD option implies that the leading/trailing spaces will be removed.

 

Why exactly would you want to include leading or trailing spaces into a delimited text file?

 

How can you know how many trailing spaces to include?  SAS character variables are fixed length.  So they are always padded with enough spaces to fill them.

 

If you know the length you want to print use the $VARYING format.  That requires a separate variable with the length to be printed.

Try this data step to see how it works.

data _null_;
  length string $10 ;
  string="   abc";
  do i=0 to 10;
    put '|' string $varying100. i '|';
  end;
run;

 

FreelanceReinh
Jade | Level 19

Hello @PRAVIN_JAIN,

 

The number of trailing spaces in variable MMN is determined by the length of that variable (and, of course, its values). So, if you want to preserve exactly those spaces and also the leading spaces while creating a delimited text file, use formatted output (at least for variable MMN) with the $CHAR. format and write the delimiter explicitly:

 

put mmn $char. '01'x rec_seq;

The default length of the $CHAR. format is the length of the variable it is applied to, which is probably convenient in your case (unless the defined length of MMN implies a different number of trailing spaces than you want).

 

Tom
Super User Tom
Super User

Are you sure you don't want to write a fixed length file? Perhaps one with extra characters between the fields to make it easier for humans to read?

data _null_;
  set sashelp.class;
  put name $char10. "|" sex $1. "|" age 4. "|" height 6.2 ;
run;

Results

Alfred    |M|  14| 69.00
Alice     |F|  13| 56.50
Barbara   |F|  13| 65.30
Carol     |F|  14| 62.80
...

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 338 views
  • 3 likes
  • 4 in conversation