DATA Step, Macro, Functions and more

Reading a program into SAS, losing indenting

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

Reading a program into SAS, losing indenting

Hi all,

 

If I have a program like this...

 

*****************************************************

THIS IS A TEST PROGRAM. 

PROJECT NUMBER: 123456

*****************************************************;

data a;
     var1 = 'hello';       
run;

**************** end of program ****************;

 

 

 

... and I want to read it into SAS to edit it with SAS, for example ...

 

** READ IN **;

data tempa;
infile "U:\test_program_orig.sas" truncover ;   ** #1 **;
input line $500. ;
run;

 

** EDIT **;

data tempb;

set tempa;

if left(upcase(line)) =: 'PROJECT NUMBER' then line = ' Project Number: ' || '09'x || "234567";

run;

 

** OUTPUT **;

data _null_;

set tempb;

file "U:\test_program_new.sas";

run;

 

During the READ IN (#1) step, I lose all my indenting (like in the var1= 'hello' statement).  Is there an option to retain leading blanks?

 

Thank you!

 

 

 

 

 

 


Accepted Solutions
Solution
‎10-31-2016 02:39 PM
Super User
Posts: 3,101

Re: Reading a program into SAS, losing indenting

Try $char500. as your informat.

View solution in original post


All Replies
Solution
‎10-31-2016 02:39 PM
Super User
Posts: 3,101

Re: Reading a program into SAS, losing indenting

Try $char500. as your informat.

Contributor
Posts: 74

Re: Reading a program into SAS, losing indenting

Excellent.  Thank you!

Contributor
Posts: 74

Re: Reading a program into SAS, losing indenting

Follow-up to this...

The CHARxx. format gives you lines with lots of trailing blanks.  I discovered that VARYING works well to avoid that. 

 

eol = findc(line, ' ' ,'v', -200);        * end of line: v -> find first non-blank, -200 -> R to L *;
if eol = 0 then eol = 1;
put line $varying200. eol;

 

Someone might find this useful.

Respected Advisor
Posts: 3,777

Re: Reading a program into SAS, losing indenting


evp000 wrote:

Follow-up to this...

The CHARxx. format gives you lines with lots of trailing blanks.  I discovered that VARYING works well to avoid that. 

 

eol = findc(line, ' ' ,'v', -200);        * end of line: v -> find first non-blank, -200 -> R to L *;
if eol = 0 then eol = 1;
put line $varying200. eol;

 

Someone might find this useful.


 

You might consider the LENGTH function for finding the length of a character variable.

Contributor
Posts: 74

Re: Reading a program into SAS, losing indenting

Length takes off the leading blanks and I want to keep them. 

Contributor
Posts: 74

Re: Reading a program into SAS, losing indenting

Oh, no it doesn't get rid of the leading blanks, so ya, that would work too.  Thanks. 

Respected Advisor
Posts: 3,777

Re: Reading a program into SAS, losing indenting

You can do it all in one step.

 

data _null_;
   infile cards;
   input;
   if left(upcase(_infile_)) =: 'PROJECT NUMBER' then _infile_ = ' Project Number: ' || '09'x || "234567";
   file log;
   put _infile_;
   cards4;
*****************************************************
THIS IS A TEST PROGRAM. 
PROJECT NUMBER: 123456
*****************************************************;
data a;
     var1 = 'hello';       
run;
**************** end of program ****************;
;;;;
   run;

 

25         data _null_;
26            infile cards;
27            input;
28            if left(upcase(_infile_)) =: 'PROJECT NUMBER' then _infile_ = ' Project Number: ' || '09'x || "234567";
29            file log;
30            put _infile_;
31            cards4;

*****************************************************                           
THIS IS A TEST PROGRAM.                                                         
 Project Number: 	234567
*****************************************************;                          
data a;                                                                         
     var1 = 'hello';                                                            
run;                                                                            
**************** end of program ****************;  
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 333 views
  • 1 like
  • 3 in conversation