View previous topic :: View next topic |
Author |
Message |
mark37 Beginner
Joined: 28 Sep 2006 Posts: 4 Topics: 1
|
Posted: Fri Mar 09, 2007 10:53 am Post subject: SAS - How do I read multiple records from Raw Data |
|
|
Can anyone help with my SAS problem please?
I am trying to read the first record of data in a qsam sequential mainframe file, identify if in cols 3-4 characters are CT. If they are then I collect data for my variables from this record. I then want to read the next record and if the cols 3-4 are FI, collect data from this record also and append to existing variables. I then need to disregard the input records if they have met the criteria, i.e. if first record = CT and second record FI disregard both, if first record = CT and second record = SI disregard first record only, if first record is not equal to CT disregard and continue with next record. This process should repeat with the rest of the records until the end of file.
The reason I need to read both records is because the information I require from the CT record is sometimes truncated. When this happens the full data portion is written in the next FI record.
The file has different values in cols 3-4, example :-
SI
XI
CT
CT
FI
GI
SI
CT
FI
CT
FI
DI
XI
However, note that FI can only ever appear immediately after a CT record.
I have tried numerous code to no avail, any pointers would be appreciated. I could not find any examples in the database similar to what I required, apologies if a similar enquiry has already been posted. |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Fri Mar 16, 2007 8:36 am Post subject: |
|
|
Can you clarify this for me.
If rec 1 = CT & rec 2 = FI then process from both records.
If rec 1 = CT & rec 2 <> FI then process from rec 2 only. _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Fri Mar 16, 2007 8:56 am Post subject: |
|
|
I seem to recall this question occurring not so long ago - and did not understand it then either! _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Fri Mar 16, 2007 9:16 am Post subject: |
|
|
I think I might get what he wants. Just as well it's a quiet Friday afternoon here. Gives the brain something to do.
You'll obviously have to code your own logic to deal with all the different record type combinations, but this might help you get there.
Code: |
DATA OUT01;
RETAIN LASTREC ' ';
FILE xxxxx;
INPUT @03 REC $2. @;
IF _N_ = 1 THEN XX = 1;
ELSE XX = 0;
IF REC = 'CT' THEN DO;
LASTREC = 'CT';
INPUT @pos var att
@pos var att
@pos var att;
END;
IF LASTREC = 'CT' AND XX = 0 THEN DO;
INPUT @03 REC $2. @;
LASTREC = REC;
IF REC = 'aa' THEN DO;
INPUT @nn var att;
END;
ELSE IF REC = 'bb' THEN DO;
INPUT @nn var att;
END;
ELSE IF REC = 'cc' THEN DO;
INPUT @nn var att;
END;
END;
|
_________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Fri Mar 16, 2007 9:21 am Post subject: |
|
|
I KNOW I've seen it before - but cannot find it. Just ignore me - it's the safest thing to do anyway  _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
 |
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Fri Mar 16, 2007 9:22 am Post subject: |
|
|
I know - where I've seen it before - it was here - it has just taken a week to get answer. Time for the weekend I think! _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Fri Mar 16, 2007 9:24 am Post subject: |
|
|
In fact I think this is better, as it will only get the next record following a 'CT' record. I need another coffee or three
Code: |
DATA OUT01;
FILE xxxxx;
INPUT @03 REC $2. @;
IF REC = 'CT' THEN DO;
LASTREC = 'CT';
INPUT @pos var att
@pos var att
@pos var att;
INPUT @03 REC $2. @;
IF REC = 'aa' THEN DO;
INPUT @nn var att;
END;
ELSE IF REC = 'bb' THEN DO;
INPUT @nn var att;
END;
ELSE IF REC = 'cc' THEN DO;
INPUT @nn var att;
END;
LASTREC = REC;
END;
|
_________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Fri Mar 16, 2007 9:27 am Post subject: |
|
|
I thought it strange you saying that about seeing it before. I guessed it wasn't a duplicate post as it's the poor guys 1st post.
Know what you mean about the weekend, I just revised my code based on something I'd written before. Strike 1 was from memory, Strike 2 was a quick crib and change from my portable library.
Where about's in Dublin are you. I spent a few months with AIB out there, in Donnybrook. _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
mark37 Beginner
Joined: 28 Sep 2006 Posts: 4 Topics: 1
|
Posted: Tue Apr 17, 2007 4:47 am Post subject: |
|
|
Thanks for the prompt reply EXPAT, I have been sidetracked with other things and am only now returning to this.
To answer your question regarding when I need to read data from the raw file:-
If rec 1 = CT & rec 2 = FI then process from both records and merge into one output record.
If rec 1 = CT & rec 2 <> FI then process from rec 1 only.
If rec 1 = CT & rec 2 = CT then process from both records and create two output records.
The third example is is where I get a bit lost as in theory there could be records as follows :-
CT
CT
FI
In this scenario I need to read data from the first record and write it out as the next record is not FI. Then read/retain the second record and read in the third record, in this scenario, as record 2 is CT and 3 is FI I need to read from both records and merge the data together into one record. Reading the above input file would provide an output file containing only two records.
Does that make sense? Would the solution you have provided cater for this? |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Tue Apr 17, 2007 5:10 am Post subject: |
|
|
Hi Mark,
The code above will enter its loop on the first occurance of a CT record. Will process the CT record according to how you want to process it.
Within the loop, it will read the next record, and depending on what record type it is will then do what you want it to do.
The logic is there, just needs you to fill in the code now.
Just one after thought, what about if there are 3 or more CT records following each other directly ? - Ahhhh, just reread your further post again - the logic will drop out and start the third CT record as a first CT record, so will cater for that eventuality. I think  _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Tue Apr 17, 2007 5:11 am Post subject: |
|
|
Just had yet another reread, and I'll get back about the
CT
CT
FI
possibility. _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Tue Apr 17, 2007 5:52 am Post subject: |
|
|
I think this will do it. Adding an internal loop within the first CT record loop.
This should enter the loop and process the first CT record, read the next record, if it is CT then do whatever, but keeps the loop indicator set at 1, so loops to the next record and always processes on the combination of the last two record types.
Cheques payable to .................................................
Code: |
DATA OUT01;
FILE XXXXX;
INPUT @03 REC $2. @;
IF REC = 'CT' THEN DO;
INPUT @POS VAR ATT
@POS VAR ATT
@POS VAR ATT;
LOOP = 1;
DO WHILE(LOOP=1);
INPUT @03 REC $2. @;
IF REC = 'CT' THEN DO;
WRITE OUTPUT RECORD FROM ORIGINAL CT RECORD;
INPUT @NN VAR ATT; /* GET ATTRIBS FROM NEW CT REC */
LOOP = 1;
END;
ELSE IF REC = 'BB' THEN DO;
INPUT @NN VAR ATT;
LOOP = 0;
END;
ELSE IF REC = 'CC' THEN DO;
INPUT @NN VAR ATT;
LOOP = 0;
END;
END;
END;
|
_________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Tue Apr 17, 2007 6:18 am Post subject: |
|
|
The only point that you will need to pay attention to is if the very last record in the file is a CT record. You may have to use the EOF= to determine if it is, and then process accordingly.
Just remembered the major problem for doing this type of loop processing. _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
mark37 Beginner
Joined: 28 Sep 2006 Posts: 4 Topics: 1
|
Posted: Fri Apr 20, 2007 1:48 am Post subject: |
|
|
Thanks for all your help Expat, I managed to resolve this in a slightly different way to your code above, however, I will also test your solution as it looks to be more efficient. I have posted the code which I used for other patrons of this site for comparison. Thanks again for taking the time to look at this for me, If you are ever in Edinburgh I will buy you a pint or three...
[code:1:4c194d2535]
DATA WORK.XXXXXXXX ;
FORMAT DSN1 |
|
Back to top |
|
 |
expat Intermediate

Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Mon May 07, 2007 3:05 am Post subject: |
|
|
Hi Mark,
Quote: | If you are ever in Edinburgh I will buy you a pint or three... |
A rather rash promise to another IT person, there's every chance that I might take you up on that
Got any storage / ops analysis / SAS jobs going there, my contract finishes this week.
Having a look through your code, I think I see only one problem.
Code: |
IF E1 = 1 THEN GOTO END_OF_FILE ;
other lines of code
END_OF_FILE:
IF FLAG = 'CT'
THEN OUTPUT WORK.XXXXXXXX ;
STOP;
RUN;
|
What happens if the last record is not FI and the previous to last was CT ?
The way that I interpret your code is that it will write the record regardless of the type of the last record. _________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
 |
|
|