DATA Step, Macro, Functions and more

Formatting a put c $

Reply
Frequent Contributor
Posts: 83

Formatting a put c $

I have a put c $ in a program and the output looks like this all on one line as output A

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER$DATA ..\TFIT1.CSV IGNORE=W 

 

However I would like the output to look like this as two seperate lines as output B

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER

$DATA ..\TFIT1.CSV IGNORE=W

 

My question is how can I format the put c $ statement to get the output to look like output B and not output A? 

 

Trusted Advisor
Posts: 1,353

Re: Formatting a put c $

Is C  one variable only?

How long should br 1st line ?

Can be C so long that you'll want to braek it into more than two lines? If yes - what is the maximum ?

 

Here is one possibility:

 

c1 = substr(c,1, 80);  /* 80 as max length for 1st line */

c2 = substr(c,81);      /* the rest of C to be on next line */

put c1 $;

put c2 $;

 

Frequent Contributor
Posts: 83

Re: Formatting a put c $

Questions:  Is C one variable

C is one variable composed of two seperate lines of character data

 

Question: How long should be 1st line 

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER

This is no more than 100 characters

 

Question:Can C be so long that you'll want to break it into more than two lines? If yes - what is the maximum ?

 

Yes I need two lines and see response above.

 

I actually put your code into my do loop which resulted in the following output of only the second line(i.e., $DATA ..\TFIT1.CSV IGNORE=W). The first line was not output.

 

CODE:

[input c $1000.;
do i=1 to 2;
myfile='/folders/myfolders/bootnew/REF' || trim(left(put(i,8.))) || '.ctl';

file dummy filevar=myfile MOD ;
if _n_=2 then do;
if i=1 then do;
amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);
end;

c= trim(first_half) || trim(left(put(i,8.))) || last_half;
c1=substr(c,1,100);
c2=substr(c,81);

end;

put c1 $;
put c2 $;
end;
run;

 



SAS Super FREQ
Posts: 8,739

Re: Formatting a put c $

Hi, it appears to me that the presence of a $ dictates the "line". If so, then why is your INDEX function looking for an &?

cynthia
Trusted Advisor
Posts: 1,353

Re: Formatting a put c $

You were very close to your target.

Change code to:

 

[input c $1000.;
do i=1 to 2;
myfile='/folders/myfolders/bootnew/REF' || trim(left(put(i,8.))) || '.ctl';

file dummy filevar=myfile MOD ;
if _n_=2 then do;
if i=1 then do;
amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);
end;

c= trim(first_half); output;

c= trim(left(put(i,8.))) || last_half; output;

end;

Frequent Contributor
Posts: 83

Re: Formatting a put c $

When I use your code only line 1 is output as:

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER 2.CSV IGNORE=W

 

Line two does not appear.  I tried some modifications of your code and either I got only one line output or both output but on the same  line.

Trusted Advisor
Posts: 1,353

Re: Formatting a put c $

Please upload your full code and your output;

Frequent Contributor
Posts: 83

Re: Formatting a put c $

Code in brackets

[data _null_ ;

infile '/folders/myfolders/bootnew/REF.CTL' firstobs=1 obs=2 truncover;
/* INPUT CHARACTER FILE*/
input c $1000.;
do i=1 to 2;
myfile='/folders/myfolders/bootnew/REF' || trim(left(put(i,8.))) || '.ctl';

file dummy filevar=myfile MOD ;
if _n_=2 then do;
if i=1 then do;
amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);
end;

c= trim(first_half) || trim(left(put(i,8.))) || last_half;
end;
put c $;
end;
run;]

Original file

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER
$DATA ..\TFIT&i.CSV IGNORE=W

 

Output file: All on one line

$PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER $DATA ..\TFIT1.CSV IGNORE=W

 

Trusted Advisor
Posts: 1,353

Re: Formatting a put c $

It is a guess but run next code (without changes) and upload the result:

(It seems that you break line C into 2 halves and afterwards concatenate the halves back to one line)

 

data _NULL_;

infile '/folders/myfolders/bootnew/REF.CTL' firstobs=1 obs=2 truncover;

input  C $1000. ;

 

myfile='/folders/myfolders/bootnew/REF' || trim(left(put(i,8.))) || '.ctl';

file dummy filevar=myfile MOD ;

 

amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);

 

put first_half $100.;

put last_half $100.;

RUN;

 

 

Trusted Advisor
Posts: 1,353

Re: Formatting a put c $

Sorry, I missed something. Ignore my previous post.

 

Your input is made already of two lines.

You concatenate the two lines to ceate a new C line and then break it into two halves.

You broke it searching for '&' character but you expect 2 lines to start each with '$ character.

 

here your code with some changes:

 

data _null_ ;

infile '/folders/myfolders/bootnew/REF.CTL' firstobs=1 obs=2 truncover;
/* INPUT CHARACTER FILE*/
input c $1000.;
do i=1 to 2;
myfile='/folders/myfolders/bootnew/REF' || trim(left(put(i,8.))) || '.ctl';

file dummy filevar=myfile MOD ;
if _n_=2 then do;
if i=1 then do;
amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);
end;

c= trim(first_half) || trim(left(put(i,8.))) || last_half;

 

line1 = '$' || scan(c,1,'$');

line2 = '$' || scan(c,2,'$');
end;
/* put c $; *** replace to: */

put line1 $100.;

put line2 $100.;
end;
run;

Super User
Posts: 10,463

Re: Formatting a put c $


Shmuel wrote:

Sorry, I missed something. Ignore my previous post.

 


If you see the "cog" next to the upper right corner of one of your posts you can click on that and select Edit to correct your post.

Frequent Contributor
Posts: 83

Re: Formatting a put c $

I tried string statements and scans to identify the two lines as being seperate but the output always contains either both on the same line or in this case of the scan syntax only line 1 was output (i.e., $PROBLEM TWO PEAK SEQUENTIAL ZERO AND FIRST ORDER).

SAS seems to ignore the second line line2 = '$' || scan(c,2,'$');

Frequent Contributor
Posts: 83

Re: Formatting a put c $

To all:

 

I got an answer from SAS consulting and the issue was that the file was UNIX formatted and the addition of termstr=lf resolved the issue.

 

This command facilitates reading files between UNIX and Windows.

Frequent Contributor
Posts: 83

Re: Formatting a put c $

A reply from Support@SAS.com pointed out the file had a Unix format and that the input statement should contain termstr=lf which allowed the input to be read.

Ask a Question
Discussion stats
  • 13 replies
  • 561 views
  • 0 likes
  • 4 in conversation