DATA Step, Macro, Functions and more

proc export to CSV - keep leading spaces

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

proc export to CSV - keep leading spaces

Is there an option within Proc Export that will keep leading spaces if they exist in a field.

 

 


Accepted Solutions
Solution
a week ago
Super User
Posts: 10,217

Re: proc export to CSV - keep leading spaces

[ Edited ]

Write your own data step, and use the $char format:

data have;
x1 = "    xxxx";
format x1 $char8.;
x2 = 5;
run;

data _null_;
set have;
file '$HOME/sascommunity/want.csv';
if _n_ = 1 then put "x1,x2";
put x1 $char8. ',' x2;
run;

The file looks like this:

x1,x2
    xxxx,5

 

PS the simple $8. format will also work, as long as you use formatted output.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 10,217

Re: proc export to CSV - keep leading spaces

[ Edited ]

Write your own data step, and use the $char format:

data have;
x1 = "    xxxx";
format x1 $char8.;
x2 = 5;
run;

data _null_;
set have;
file '$HOME/sascommunity/want.csv';
if _n_ = 1 then put "x1,x2";
put x1 $char8. ',' x2;
run;

The file looks like this:

x1,x2
    xxxx,5

 

PS the simple $8. format will also work, as long as you use formatted output.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 3

Re: proc export to CSV - keep leading spaces

Posted in reply to KurtBremser

thanks, this will work.  However, it would be simpler if there was an option on Proc Export to preserve leading spaces/blank as additional code has to be written in order for this solution to work.

Super User
Super User
Posts: 8,089

Re: proc export to CSV - keep leading spaces

Normally leading/trailing spaces in CSV files are removed by the consumer.

Why do you want to preserve them?

New Contributor
Posts: 3

Re: proc export to CSV - keep leading spaces

the spaces need to be preserved as information in certain parts of the column have meaning.  For example, position 50 of a column has meaning but all of the other columns could be blank.  In that case, without modifying the export, the leading spaces are being trimmed thus compromising the information.

Super User
Super User
Posts: 8,089

Re: proc export to CSV - keep leading spaces

Then why not create a fixed column output file instead?

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 78 views
  • 1 like
  • 3 in conversation