DATA Step, Macro, Functions and more

Leading Blanks

Reply
Frequent Contributor
Posts: 97

Leading Blanks

I am trying to import data from a text file, and I need to keep the leading blanks. Here is what I am using:

@0021 SubmissionRecordCount $CHAR8.

Data is:

SC010024201103241201 1468

However, even with the CHAR informat, it still loses the blanks.

I would appreciate any help!

Thanks,
Chris
Christopher Johnson
www.codeitmagazine.com
New Contributor
Posts: 3

Re: Leading Blanks

One way of looking at the problem is that you're not losing the blanks, you are just left aligning your character variable. So right align it and add blanks. The RIGHT function will right-align the variable when output.
Respected Advisor
Posts: 3,777

Re: Leading Blanks

Your code is working exactly as expected. You can use the justification format modifier to right justify the value in the field.

[pre]
80 data _null_;
81 length SubmissionRecordCount $8;
82 input @0021 SubmissionRecordCount $CHAR8.-R;
83 put '12345678';
84 put (SSmiley Happy ($char20.);
85 cards;

12345678
1468
[/pre]

If you change the variable length the justifcation is unexpected.

[pre]
data _null_;
length SubmissionRecordCount $20;
input @0021 SubmissionRecordCount $CHAR8.-R;
put '12345678901234567890';
put (SSmiley Happy ($char20.);
cards;
SC010024201103241201 1468
;;;;
run;[/pre]

[pre]
125 data _null_;
126 length SubmissionRecordCount $20;
127 input @0021 SubmissionRecordCount $CHAR8.-R;
128 put '12345678901234567890';
129 put (SSmiley Happy ($char20.);
130 cards;

12345678901234567890
1468
[/pre]
Trusted Advisor
Posts: 2,113

Re: Leading Blanks

I took data_null_'s code and dropped the "-R" and got what I think that cjohnson is looking for. (SAS 9.2 TS2M3).

cjohnson, If you are still having problems, maybe you can explain more fully. Another way to debug this is to print the data with the hex format so you can precisely tell what is in there.

Doc Muhlbaier
Duke

data _null_;
length SubmissionRecordCount $20;
input @0021 SubmissionRecordCount $CHAR8.;
put '12345678901234567890';
put (SSmiley Happy ($char20.);
cards;
SC010024201103241201 1468
;;;;
run;

10 data _null_;
11 length SubmissionRecordCount $20;
12 input @0021 SubmissionRecordCount $CHAR8.;
13 put '12345678901234567890';
14 put (SSmiley Happy ($char20.);
15 cards;

12345678901234567890
1468
Regular Contributor
Posts: 241

Re: Leading Blanks

The leading blanks are preserved if you use $char informat. Here is an example run on my 9.2 (TS1M0), windows platform:



data one;

  input @21 count $char8.;

cards;

----+----1----+----2----+----3

SC010024201103241201 1468

;

run;

proc print data=one;

run;

/* on lst

Obs    count

 1     ----+---

 2      1468

*/
Frequent Contributor
Posts: 97

Re: Leading Blanks

Ah...you are right. The leading blanks were being preserved. I couldn't see it in the table view, but when I also added the $char format to the export, it kept the blanks. Thanks very much!
Christopher Johnson
www.codeitmagazine.com
Ask a Question
Discussion stats
  • 5 replies
  • 205 views
  • 0 likes
  • 5 in conversation