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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 861 views
  • 2 likes
  • 5 in conversation