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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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.

mthomas0
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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

Why do you want to preserve them?

mthomas0
Calcite | Level 5

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.

Tom
Super User Tom
Super User

Then why not create a fixed column output file instead?

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
  • 5 replies
  • 2716 views
  • 1 like
  • 3 in conversation