I have to write a file using 2 input files.
INFILE IPADFILE;
INPUT @001 BEGIN_REC $CHAR30.
@031 NAME $CHAR5.
@036 MIDDLE_REC $CHAR86.
@122R_SYS $CHAR1.
@123END_REC $CHAR22.
INFILE IXREF;
INPUT @001 LOCN $CHAR5.
@006 K_LOCN $CHAR5.
@011 END_RECRD $CHAR68.;
Now I have to write output which will have same structure same as IPADFILE but while writing it i want to replace @031 NAME $CHAR5 from @006 K_LOCN $CHAR5, basically i want output file as per below structure:
PUT @001 BEGIN_REC $CHAR30.
@031 K_LOCN $CHAR5.
@036 MIDDLE_REC $CHAR86.
@122R_SYS $CHAR1.
@123END_REC $CHAR22.;
IPADFILE and IXREF are DD name of the files in JCL.
Can anyone please help.
An idea or two first ...
You are overcomplicating things by reading in all the variables. For example, if you need just K_LOCN, read just K_LOCN:
INFILE IXREF;
INPUT @006 K_LOCN $CHAR5.;
Are you sure these files match up line by line? So far, you are asking to match the first line of 1 file with the first line from the other file, then the 2nd line of 1 file with the 2nd line from the other file, etc.
Where should the output go? Do you want to replace the IPADFILE? Write to a new file? Write a report?
If you want to write to a new file, you need JCL to define that new file.
There's more to consider, but that's a good starting point.
Thanks for comment.
I need to write the output in a new file and that is defined in JCL.
The structure of first inputfile (IPADFILE) and output file is identical,
while writting the output file i need all the fields from IPADFILE except the @031 NAME $CHAR5 , i want it to be replaced by the IXREF file's @006 K_LOCN $CHAR5.
OK, for that you don't need to read all the fields from IPADFILE. Here's a neat trick:
INFILE IPADFILE;
INPUT DUMMY $ 1. @ ;
FILE NEWFILE;
PUT _INFILE_ @ ;
INFILE IXREF;
INPUT @ 6 K_LOCN $CHAR5.;
PUT @ 6 K_LOCN $CHAR5.;
Test it to make sure the information matches up properly line by line.
_INFILE_ refers to an exact copy of the most recent raw data line, so you don't need to break it into pieces. Just write out the whole thing, then overwrite 5 characters of that with the contents of K_LOCN.
You don't have to type your SAS code in uppercase (its not JCL).
If you know the records are in the same order then you should be able to just overwrite the characters starting at column 31 with the string from the record in the other file.
// EXEC SAS
//IPADFILE DD ...
//IXREF DD ...
//NEWFILE DD ...
//SYSIN DD *
data _null_;
file newfile ;
infile ipadfile;
input ;
put _infile_ @ ;
infile ixref;
input @6 string $char5.;
put @31 string $char5.;
run;
If you really want the file to look exactly the same you might need to specify the file attributes (RECFM, LRECL, etc) for the output file in the JCL. Isn't there a way in JCL to copy the attributes from another file? Or perhaps you could set them in the FILE statement of the SAS code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.