0

I have a data set that I have created and it needs to be formatted to have trailing/padded spaces to the specified number of characters.

Below are the field names from the STG data set.

'Social Security Number (SSN)'n
'Date of Birth'n 
'Last Name'n 
'First Name'n 

SSN must start at position 1 and only be 9 characters long (position 1-9)

DOB must start at position 10 and be 8 characters long (position 10-17)

Last Name must start at position 18 and be 26 characters long (position 18-43)

First Name must start at pisition 44 and be 20 characters long (position 44-63)

so on and so forth.

When exported, I need to be able to see the padded/trailing spaces stored in the string. All my fields are formatted as character.

What is the best way to approach this? I've tried specify length and subpad in a data step but when exported, the trailing spaces are lost.

Mark
  • 327
  • 1
  • 7
  • 14

1 Answers1

1

It is pretty trivial to generate a fixed column text file. Just use the PUT statement.

You can force fixed length lines by writing a space into the last column before writing the actual values. That will insure that all lines are padded to at least that length.

 data _null_;
    file 'mydata.txt'  ;
    set have;
    put @63 ' '
      @ 1 SSN $9.
      @10 DOB yymmdd8.
      @18 LAST_NAME $26.
      @44 FIRST_NAME $20.
    ;
 run;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • Thank you Tom. As stated previously, it needs to be uploaded to a site which requires the format to be as such. I tried the above and added the additional fields I needed, however I am receiving an error. ERROR 23-2: Invalid option name RECMF. Thoughts? – Mark Oct 03 '19 at 18:05
  • 1
    Just fix my typo and the code should work. Note you probably do not to include RECFM= option if you are explicitly writing using formats to fill every column. – Tom Oct 03 '19 at 18:12
  • Thank you. I think I'm almost there. I was expecting to see each record on a separate row, however they seem to run on one record after another when opened in a text editor. Meaning the second record isn't on a new line, it starts right after the first. Is there a way to start each record on a separate line? Not a big deal. Just something aesthetic the end user is asking for. – Mark Oct 03 '19 at 18:41
  • 1
    I would remove RECFM=F option. Let me update the answer with a different method to insure all lines are same length. – Tom Oct 03 '19 at 18:54
  • Thank you Tom. I've changed the code to your updated statement however there is now a . showing at the very end of each record. The records are also still not on a separate row as expected. – Mark Oct 03 '19 at 19:01
  • A SAS statement starts with a keyword (such as `PUT`) and ends with a semi-colon. @Tom broke the statement into four lines of source code for easy reading. To have each field on a separate row (weird ?) you can add the line advance put directive (`/`) to the source code before the 2nd, 3rd and 4th `@` signs. I think `$CHAR20.` will force trailing spaces in last field – Richard Oct 03 '19 at 19:04
  • One way the records would be merged would be because of leaving a trailing @ on the PUT statement. That will stop SAS from writing the end of line. Another way they could appear merged is if SAS wrote the files using LF as the end-of-line marker and the editor you are using is looking for CR+LF as the end of line marker. You could try adding TERMSTR=CRLF to the FILE statement. Extra dots will be caused by printing a variable with a missing value. Probably a typo like a misspelled name, leaving the period off of the format specification so it looks like another variable. – Tom Oct 03 '19 at 19:13
  • Adding TERMSTR=crlf to my file statement gave me a return as expected. Thank you for your assistance! – Mark Oct 07 '19 at 16:23