BookmarkSubscribeRSS Feed
jacksonan123
Lapis Lazuli | Level 10

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? 

 

13 REPLIES 13
Shmuel
Garnet | Level 18

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 $;

 

jacksonan123
Lapis Lazuli | Level 10

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;

 



Cynthia_sas
SAS Super FREQ
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
Shmuel
Garnet | Level 18

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;

jacksonan123
Lapis Lazuli | Level 10

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.

Shmuel
Garnet | Level 18

Please upload your full code and your output;

jacksonan123
Lapis Lazuli | Level 10

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

 

Shmuel
Garnet | Level 18

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;

 

 

Shmuel
Garnet | Level 18

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;

ballardw
Super User

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

jacksonan123
Lapis Lazuli | Level 10

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,'$');

jacksonan123
Lapis Lazuli | Level 10

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.

jacksonan123
Lapis Lazuli | Level 10

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.

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
  • 13 replies
  • 1434 views
  • 0 likes
  • 4 in conversation