BookmarkSubscribeRSS Feed
DougZ
Obsidian | Level 7

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;

 

10 REPLIES 10
art297
Opal | Level 21
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

 

DougZ
Obsidian | Level 7
Art - thanks. That works too.
Reeza
Super User

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;
 
DougZ
Obsidian | Level 7
Reeza - this makes sense, not sure why I didn't use a $char80. - thanks for your solution!
Astounding
PROC Star

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
;

DougZ
Obsidian | Level 7

Thanks to each of you who has posted a solution! 

novinosrin
Tourmaline | Level 20
data test2;
    infile cards ;
    input @;
    line=_infile_;
    cards;
The xyz
    %mymacro
    myteststring
;
run;
DougZ
Obsidian | Level 7
That's great. Not sure why that works and my code did not.
Reeza
Super User

The link in my post has an explanation. 

novinosrin
Tourmaline | Level 20

@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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 872 views
  • 2 likes
  • 5 in conversation