SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Linglight
Calcite | Level 5

Hi,

 

Currently I'm trying to export a dataset to a CSV and I'm having issues with one of the character fields.

 

Say I have a character field 'Test' of length 10 with the only values for it being "ABCDEFGHI "

 

Note that the value has a space at the end of it. The issue is after I export it to CSV using proc export the resulting file doesn't have the space within the values of 'Test'. It would just have the value of "ABCDEFGHI"

 

Is there a way to export the field with the trailing space? 

 

Things I have noted:

I have already tried doing format = $10. The issue seems to be that even though the field has a length of 10, in SAS it will treat it as BLANK instead of a space character.

 

Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you need a FIXED width file, not a delimited file.

You can easily insert commas into the file if you want.

filename out temp;
data _null_;
  set sashelp.class ;
  file out ;
  put name $20. ',' age 3. ',' sex $1. ',' ;
run;
data _null_;
  infile out;
  input;
  put _infile_;
run;

Results:

Alfred              , 14,M,
Alice               , 13,F,
Barbara             , 13,F,
Carol               , 14,F,
Henry               , 14,M,
James               , 12,M,
Jane                , 12,F,
Janet               , 15,F,
Jeffrey             , 13,M,
John                , 12,M,
Joyce               , 11,F,
Judy                , 14,F,
Louise              , 12,F,
Mary                , 15,F,
Philip              , 16,M,
Robert              , 12,M,
Ronald              , 15,M,
Thomas              , 11,M,
William             , 15,M,

But I would not call such a file a CSV file.  It is not.

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Are you saying you somehow what to treat 'AB   ' as different than 'AB' ?  Why ?  That will cause all sorts of problems down the line.

If you want to treat trailing spaces in a text field as meaningful then you will need to either add another variable to store the total length or append an extra non blank character to the end of all of the values to mark the end.

 

Plus CSV files do not store meaningful leading or trailing spaces in fields.

Linglight
Calcite | Level 5

Unfortunately its not something I decided. Another team requires the file to have very specific lengths. One of the criteria is that character fields must have trailing spaces if they have a lower length.

 

Is there a way to adjust the proc export step to not remove the trailing spaces? I don't understand why they drop off at all cause the actual value of the field is 'AB '. If you ran tranwrd('AB ',' ','1') it would return 'AB1'. Its just not 'visible' for the proc export for some reason.

 

Thanks 

 

 

Tom
Super User Tom
Super User

Sounds like you need a FIXED width file, not a delimited file.

You can easily insert commas into the file if you want.

filename out temp;
data _null_;
  set sashelp.class ;
  file out ;
  put name $20. ',' age 3. ',' sex $1. ',' ;
run;
data _null_;
  infile out;
  input;
  put _infile_;
run;

Results:

Alfred              , 14,M,
Alice               , 13,F,
Barbara             , 13,F,
Carol               , 14,F,
Henry               , 14,M,
James               , 12,M,
Jane                , 12,F,
Janet               , 15,F,
Jeffrey             , 13,M,
John                , 12,M,
Joyce               , 11,F,
Judy                , 14,F,
Louise              , 12,F,
Mary                , 15,F,
Philip              , 16,M,
Robert              , 12,M,
Ronald              , 15,M,
Thomas              , 11,M,
William             , 15,M,

But I would not call such a file a CSV file.  It is not.

Linglight
Calcite | Level 5

Yea you're right. I think I was a bit hung up on using proc export as a csv to try and get it to work.

 

The code you provided to insert commas worked nicely.

 

Thanks! 

Tom
Super User Tom
Super User

SAS datasets have two types of variables. Fixed length character strings and floating point numbers.

If you want your program to have one variable named NAME that some values that are 3 character long and others that are 5 you cannot do that. When you store it into the variable the value is padded with spaces to fill the fixed length.

 

Now you can write a text file in format you want.  But if you really have to know to write only 3 characters for the first observation and 5 for the second then you need to store the 3 and the 5 somewhere.  So if you have a variable named NAME that is defined as $10 and a second variable named NAME_LENGTH that has how many of those characters to use then you can use the $VARYING format to write the different length strings for the different observations.

data _null_;
  set have;
  file out;
  put name $varying10. name_length ....
run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2698 views
  • 2 likes
  • 2 in conversation