DATA Step, Macro, Functions and more

SAS automatically left-justifying input text line?

Reply
Contributor
Posts: 20

SAS automatically left-justifying input text line?

When I read my input text line, SAS thinks it starts in column 1... but I need to write out another text file with the same indentation that was found in that input text file.

 

data test;
    infile cards;
    input line: $1-80;
    line_begn_posn = notspace(line);
    put line= line_begn_posn=;
    cards;
The xyz
    %mymacro
    myteststring
;
run;

 

PROC Star
Posts: 8,145

Re: SAS automatically left-justifying input text line?

data test;
    infile cards;
    input @1 line $char80.;
    line_begn_posn = notspace(line);
    put line= line_begn_posn=;
    cards;
The xyz
    %mymacro
    myteststring
;
run;

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 20

Re: SAS automatically left-justifying input text line?

Art - thanks. That works too.
Super User
Posts: 23,235

Re: SAS automatically left-justifying input text line?

https://communities.sas.com/t5/General-SAS-Programming/Keep-leading-spaces-and-blank-records/td-p/17...

 

This works for me:

 

data test;
    infile cards;
    input @1 line $char80.;
    line_begn_posn = notspace(line);
    put line= line_begn_posn=;
    cards;
The xyz
    %mymacro
    myteststring
;
run;
 
Contributor
Posts: 20

Re: SAS automatically left-justifying input text line?

Reeza - this makes sense, not sure why I didn't use a $char80. - thanks for your solution!
Super User
Posts: 6,626

Re: SAS automatically left-justifying input text line?

Once you read the value in with the leading blanks ($char80 informat), you can write it out the same way:

 

data test;
    infile cards;
    input line $char80.;

    put line $char80.;
    cards;
The xyz
    %mymacro
    myteststring
;

Contributor
Posts: 20

Re: SAS automatically left-justifying input text line?

Posted in reply to Astounding

Thanks to each of you who has posted a solution! 

PROC Star
Posts: 1,558

Re: SAS automatically left-justifying input text line?

data test2;
    infile cards ;
    input @;
    line=_infile_;
    cards;
The xyz
    %mymacro
    myteststring
;
run;
Contributor
Posts: 20

Re: SAS automatically left-justifying input text line?

Posted in reply to novinosrin
That's great. Not sure why that works and my code did not.
Super User
Posts: 23,235

Re: SAS automatically left-justifying input text line?

The link in my post has an explanation. 

PROC Star
Posts: 1,558

Re: SAS automatically left-justifying input text line?

[ Edited ]

@DougZ interesting question indeed.

1.Let me add some spice to the &char80. informat. Place a : (colon) if you were to read as modified list input coz not every record will have values of 80 chars. And in this case we go back to square losing our needed leading blanks 

2. ok, if we choose  to read with formatted input and NOT modified list input, you are running a risk of reading the next value supposing there are multiple vars to be read

3. Taking 1 and 2 makes this situation indeed tricky. Of course, this leads us to think of next options like $varying informat and use col= options and so on.

Ask a Question
Discussion stats
  • 10 replies
  • 100 views
  • 2 likes
  • 5 in conversation