10. The raw data file Mixed_Recs.txt contains two types of records. Records with a 1 in Column 16
contain sales records with Date (in mmddyy10. form) starting in Column 1 and Amount in Columns
11–15 (in standard numeric form). Records with a 2 in Column 16 are inventory records and they
contain two values, a part number (character, 5 bytes) starting in column 1 and a quantity. These two
values are separated by a space. A listing of this file is shown below:
10/21/2005 1001
11/15/2005 2001
A13688 250 2
B11112 300 2
01/03/2005 50001
A88778 19 2
Write a DATA step to read this file and create two SAS data sets, one called Sales and the other
Inventory. The two data sets should look like this:
data sales(keep=saleDate Amount) inventory(keep=partno Quantity);
infile '/home/wckwone/SASbyExample/mixed_recs.txt' truncover;
input @16 type 1. @;
if type=1 then do;
input @1 saleDate mmddyy10. Amount 11-15;
output sales;
end;
else do;
input partno $1-6 Quantity 8-11;
output Inventory;
end;
run;
KEEP is a declarative statement that affects all output datasets and is dealt with at compile time. So it is not executed, meaning it cannot be used conditionally.
Use KEEP= dataset options in the DATA statement instead.
data sales(keep=saleDate Amount) inventory(keep=partno Quantity);
infile '/home/wckwone/SASbyExample/mixed_recs.txt' truncover;
input @16 type 1. @;
if type=1 then do;
input @1 saleDate mmddyy10. Amount 11-15;
output sales;
end;
else do;
input partno $1-6 Quantity 8-11;
output Inventory;
end;
run;
So what is your question now?
If you receive messages in the log that puzzle you, or the result does not match your expectations, post the complete log by copy/pasting it into a window opened with this button:
Similarly, post a few lines of your input data, using the same method.
@Kurt_Bremser Initially, I have problems dropping the unnecessary columns. Your Suggestion have solve the issue.
Thanks a lot.
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.