BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dennis_K
Obsidian | Level 7

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:

1 ACCEPTED SOLUTION

Accepted Solutions
Dennis_K
Obsidian | Level 7

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;

View solution in original post

5 REPLIES 5
Dennis_K
Obsidian | Level 7
my code is as follows:

data sales inventory;
infile '/home/wckwone/SASbyExample/mixed_recs.txt' truncover;
input @16 type 1. @;

if type=1 then do;
keep saleDate Amount;
input @1 saleDate mmddyy10. Amount 11-15;
output sales;


end;

else do;
keep partno Quantity ;

output Inventory;

end;

drop type;
run;
Kurt_Bremser
Super User

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.

Dennis_K
Obsidian | Level 7

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;

Kurt_Bremser
Super User

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:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

Similarly, post a few lines of your input data, using the same method.

 

Dennis_K
Obsidian | Level 7

@Kurt_Bremser Initially, I have problems dropping the unnecessary columns. Your Suggestion have solve the issue.

Thanks a lot.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 936 views
  • 1 like
  • 2 in conversation