ID Number generation from the external file

Reply
N/A
Posts: 0

ID Number generation from the external file

I have the Input file in this format

06325 00010 00020
06323 01001 01006
06322 02000 00000

In need to generate the multiple observation from the each input observation.
The first column is the date in the Julian format and the second and third columns
are AGE1 and AGE2 respectively. Age1 and Age2 give the range. When the Age2 is zero the output will have only one ID number with Age1 as the second 5 byte character.
The Output should be to the external file in the format.
/* start of first observation generation of ID numbers
0632500010
0632500011
;;
;;
0632500019
0632500020
/* start of second observation generation of receipt numbers
0632301001
;;
;;
0632301006
/* start of third observation generation of receipt numbers
0632202000
SAS Super FREQ
Posts: 8,643

Re: ID Number generation from the external file

Hi:
Let's assume that your input data was in a SAS dataset, called TESTDATA, as you described in your post, with the variables DATE, AGE1 and AGE2 (DATE is shown here in Julian form, although, internally, it would not be stored as a JULIAN date in the SAS dataset):
[pre]
06325 00010 00020
06323 01001 01006
06322 02000 00000
[/pre]

In that case, this program with a simple do loop can create an output file called C:\TEMP\INFO_OUT.TXT, as you described:
[pre]

data _null_;
set testdata;
file 'c:\temp\info_out.txt';
do i = age1 to age2 by 1;
put @1 date julian5. i z5.;
end;
if age2 = 00000 then do;
put @1 date julian5. age1 z5.;
end;
run;
[/pre]

The AGE1 variable is used as the start value for the DO loop and the AGE2 variable is used as the stop value for the DO loop. For every time through the LOOP, the PUT statement will write a line to the file defined by the FILE statement, using the variables provided in the PUT statement. In the case of your 3rd row, AGE1 is 2000 and AGE2 is 0 -- so the loop would never execute for that row -- the loop can't go from 2000 to 0 by 1. However, the IF statement then comes into play. since Age2 is 0, then a different PUT statement is executed for that record, where the date is written, followed by the value for AGE1.

If, on the other hand, your INPUT file is a "flat" file or "raw text" file, (not a SAS dataset) and you want to read it in and then immediately write out the new datalines, your program would look something like this:
[pre]
data _null_;
infile 'c:\temp\input_file.txt';
input date : julian. age1 age2;

file 'c:\temp\output_file.txt';
do i = age1 to age2 by 1;
put @1 date julian5. i z5.;
end;
if age2 = 00000 then do;
put @1 date julian5. age1 z5.;
end;
run;
[/pre]

In this program, the INFILE statement is pointing to the "raw" file, c:\temp\input_file.txt, while the INPUT statement is parsing each dataline and putting the values from the dataline into variables. Then the FILE statement is pointing to the ASCII text output file that is being created, c:\temp\output_file.txt -- and the DO loops and IF statement are the same as in the first example to write the new datalines to the new output file.

If you need more help with this task, then your best bet is to contact SAS Technical Support for help with INFILE/FILE and INPUT/PUT statements.

cynthia
N/A
Posts: 0

Re: ID Number generation from the external file

just in order to show the (almost) incredible flexibility of the SAS System Data Step language, here is a small variation to Cynthia's data step which overcomes the restriction
"In the case of your 3rd row, AGE1 is 2000 and AGE2 is 0 -- so the loop would never execute for that row -- the loop can't go from 2000 to 0 by 1."

Here is the data step with the alternate loop:[pre]data _null_;
set testdata;
file 'c:\temp\info_out.txt';
do age = age1 by 1 until( age >= age2);
put @1 date julian5. age z5.;
end;
run;[/pre]
[pre] if age2 = 00000 then do;[/pre] is not needed, because a row will always be output, because the exit from the loop is tested after the first pass of the loop is completed.

PeterC
N/A
Posts: 0

Re: ID Number generation from the external file

Thanks for the feedback.
Post a Question
Discussion Stats
  • 3 replies
  • 346 views
  • 0 likes
  • 2 in conversation