Hi,
Here is the code:
------------------
/* Chapter 21
Problem 21.10*/
Libname Learn'/folders/myfolders/SASData' ;
Data Prob21_10 ;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
input @16 Code 2. @ ;
If Code eq 2 then
Output = Sales ;
input @1 PartNumber $ 5.
@8 Quantity 3. ;
Else if Code ne 2 then
Output = Inventory ;
input @1 Date mmddyy10.
@11 Amount 5. ;
run ;
-----------------------
Here is the Log:
See changes in red:
data SALES INVENTORY;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
format DATE mmddyy10. ;
input @16 Code 2. @ ;
If Code eq 2 then do;
output SALES ;
input @1 PartNumber $ 5.
@8 Quantity 3. ;
end ;
else if Code ne 2 then do;
output INVENTORY ;
input @1 Date mmddyy10.
@11 Amount 5. ;
end ;
run ;
You need do blocks.
if Code eq 2 then do;
Output = Sales ;
input @1 PartNumber $ 5. @8 Quantity 3. ;
end;
else do;
Output = Inventory ;
input @1 Date mmddyy10. @11 Amount 5. ;
end;
sas ready sales variable here
what value we get in sales variable
Data Prob21_10 ;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
input @16 Code 2. @ ;
If Code eq 2 then
Output = Sales ; /* the then branch ends here, and any else must immediately follow it */
input @1 PartNumber $ 5.
@8 Quantity 3. ;
Else if Code ne 2 then /* to make this else valid, the previous 2 statements need to be enclosed in a do-end block */
Output = Inventory ;
input @1 Date mmddyy10.
@11 Amount 5. ;
run ;
Ok. That makes sense. The do loop needs to be included, and this works. Thanks.
There is one more important thing... I need to create separate printouts for Sales and Inventory. In the past , when using an if... then statement, without the do loop, the output created a new data set I could then print. But this does not work when the output= is used withing a do loop, Also, when I try to create the output sets, that is not working either. Finally, the mmddyy10. format does not do anything. The Date in the output window stays in the SAS numerical form, rather than the desired format. This has been frustrating. Any help would be appreciated.
Below is my adjusted code:
--------------------------------
/* Chapter 21
Problem 21.10*/
Libname Learn'/folders/myfolders/SASData' ;
Data Prob21_10 ;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
input @16 Code 2. @ ;
If Code eq 2 then do
Output = Sales ;
input @1 PartNumber $ 5.
@8 Quantity 3. ;
end ;
Else if Code ne 2 then do
Output = Inventory ;
input @1 Date mmddyy10.
@11 Amount 5. ;
format Data mmddyy10. ;
end ;
run ;
Data Mixed ;
Set Work.Prob21_10 ;
If Code eq 2 then Output Sales ;
Else if Code ne 2 then Output Inventory ;
run ;
Title 'Sales' ;
proc print data=Sales noobs ;
run ;
Title 'Inventory' ;
proc print data=Inventory noobs ;
run ;
-------------------------------------------
Below is the Log:
See changes in red:
data SALES INVENTORY;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
format DATE mmddyy10. ;
input @16 Code 2. @ ;
If Code eq 2 then do;
output SALES ;
input @1 PartNumber $ 5.
@8 Quantity 3. ;
end ;
else if Code ne 2 then do;
output INVENTORY ;
input @1 Date mmddyy10.
@11 Amount 5. ;
end ;
run ;
Thanks. I tried that. Here are the results I am getting: Everything is in the right place, but both have a title of inventory.
Inventory
Code Output Sales PartNumber Quantity Inventory Date Amount
1 | . | . | . | . | 10/21/2005 | 100 | |
1 | . | . | . | . | 11/15/2005 | 200 | |
2 | . | . | A1368 | 250 | . | . | . |
2 | . | . | B1111 | 300 | . | . | . |
1 | . | . | . | . | 01/03/2005 | 5000 | |
2 | . | . | A8877 | 19 | . | . | . |
Inventory
Code Output Sales PartNumber Quantity Inventory Date Amount
1 | . | . | . | . | 10/21/2005 | 100 | |
1 | . | . | . | . | 11/15/2005 | 200 | |
2 | . | . | A1368 | 250 | . | . | . |
2 | . | . | B1111 | 300 | . | . | . |
1 | . | . | . | . | 01/03/2005 | 5000 | |
2 | . | . | A8877 | 19 | . | . | . |
look at the table called SALES
Thanks. I am not sure why I did not see the Sales table before, now when I retyped the code I see it. Anyway, it works. I'm getting tired (it's late here). Thanks for your help!.
a) Please, and I mean PLEASE!, start posting logs with the {i} button, so that the text formatting of logs is preserved.
b) The ERROR message
ERROR 455-185: Data set was not specified on the DATA statement.
can't be made any more descriptive. All datasets that are used in an output statement must also appear in the data statement itself.
Number one, thanks for explicitly stating that all output files must also be named in the data step. I have been learning SAS over the past few months from scratch from the book Learning Sas by Example. While it teaches a lot, it did not explicitly state this. Now that I go through the chapters I see that all output data sets were first named in the data step, but I didn't know that was required until now.
Second, regarding the logs, and {i}, could you please expound on that? What I have done is to copy the logs from SAS University, OpenBox (on Windows). When I do this, it tells me that the HTML is invalid, and removes it, before I can post. I tried to hit CTRL i but that did not do anything. How exactly do I get the information from the log properly transferred onto this board? Tx.
See the instructions here: https://communities.sas.com/t5/Getting-Started/How-to-add-SAS-syntax-to-your-post/ta-p/224394
The {I} button is right left to the "little running man" button for posting code.
Great. Thanks for the info on {i}. Below is just a test to make sure this is done correctly. 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 61 62 /* Chapter 21 63 Problem 21.10*/ 64 Libname Learn'/folders/myfolders/SASData' ; NOTE: Libref LEARN was successfully assigned as follows: Engine: V9 Physical Name: /folders/myfolders/SASData 65 66 Data Sales Inventory ; 67 infile '/folders/myfolders/SASData/mixed_recs.txt' pad ; 68 input @16 Code 2. @ ; 69 If Code eq 2 then do ; 70 Output = Sales ; 71 input @1 PartNumber $ 5. 72 @8 Quantity 3. ; 73 end ; 74 75 76 Else if Code ne 2 then do ; 77 Output = Inventory ; 78 input @1 Date mmddyy10. 79 @11 Amount 5. ; 80 format Date mmddyy10. ; 81 end ; 82 run ; NOTE: Variable Sales is uninitialized. NOTE: Variable Inventory is uninitialized. NOTE: The infile '/folders/myfolders/SASData/mixed_recs.txt' is: Filename=/folders/myfolders/SASData/mixed_recs.txt, Owner Name=sasdemo,Group Name=sas, Access Permission=-rw-rw-r--, Last Modified=23Aug2017:10:33:41, File Size (bytes)=106 NOTE: 6 records were read from the infile '/folders/myfolders/SASData/mixed_recs.txt'. The minimum record length was 16. The maximum record length was 16. NOTE: The data set WORK.SALES has 6 observations and 8 variables. NOTE: The data set WORK.INVENTORY has 6 observations and 8 variables. NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.01 seconds 83 84 85 86 Title 'Sales' ; 87 proc print data=Sales noobs ; 88 run ; NOTE: There were 6 observations read from the data set WORK.SALES. NOTE: PROCEDURE PRINT used (Total process time): real time 0.10 seconds cpu time 0.09 seconds 89 90 Title 'Inventory' ; 91 proc print data=Inventory noobs ; 92 run ; NOTE: There were 6 observations read from the data set WORK.INVENTORY. NOTE: PROCEDURE PRINT used (Total process time): real time 0.07 seconds cpu time 0.07 seconds
One last thing on this, and I'm done... Ok, now I want to get rid of the columns that do not pertain to inventory or sales. I have tried to use drop but it gets rid of all the data. Below is my code: --------------------------- /* Chapter 21 Problem 21.10*/ Libname Learn'/folders/myfolders/SASData' ; Data Sales Inventory ; infile '/folders/myfolders/SASData/mixed_recs.txt' pad ; Input @16 Code 2. @ ; If Code eq 1 then do ; Out = Sales ; input @1 Date mmddyy10. @11 Amount 5. ; format Date mmddyy10. ; Drop Out ; end ; Else if Code ne 1 then do ; Out = Inventory ; input @1 PartNumber $ 8. @9 Quantity 3. ; Drop Out ; end ; run ; Title 'Sales' ; proc print data=Sales noobs ; run ; Title 'Inventory' ; proc print data=Inventory noobs ; run ; ------------------------------------------ Below are the Results: Sales Code Sales Date Amount Inventory PartNumber Quantity 1 . 10/21/2005 100 . . 1 . 11/15/2005 200 . . 2 . . . . A13688 2 50 2 . . . . B11112 3 0 1 . 01/03/2005 5000 . . 2 . . . . A88778 1 9 Inventory Code Sales Date Amount Inventory PartNumber Quantity 1 . 10/21/2005 100 . . 1 . 11/15/2005 200 . . 2 . . . . A13688 2 50 2 . . . . B11112 3 0 1 . 01/03/2005 5000 . . 2 . . . . A88778 1 9 I want to get rid of the Inventory and Sales columns altogether, and get rid of the Partnumber and Quantity columns for Sales, while getting rid of the Date and Amount Column for the inventory listing. This one problem has taken a lot of time, much much more than most. Hopefully what is learned from this will help a lot in the future. Tx!
Use selective dataset options, not a global drop statement, and drop/keep the correct variables:
Data
Sales (keep=Code Date Amount)
Inventory (keep=Code PartNumber Quantity)
;
format Date mmddyy10.;
infile '/folders/myfolders/SASData/mixed_recs.txt' pad;
Input @16 Code 2. @;
If Code eq 1 then do;
input
@1 Date mmddyy10.
@11 Amount 5.
;
output Sales;
end;
Else do; /* no if-then needed here with the condition you used */
input
@1 PartNumber $ 8.
@9 Quantity 3.
;
output Inventory;
end;
run;
You might also consider to not keep Code, as it is redundant in each dataset
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.