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

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:

 

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 Prob21_10 ;
67 infile '/folders/myfolders/SASData/mixed_recs.txt' pad ;
68 input @16 Code 2. @ ;
69 If Code eq 2 then
70 Output = Sales ;
71 input @1 PartNumber $ 5.
72 @8 Quantity 3. ;
73
74
75 Else if Code ne 2 then
____
160
ERROR 160-185: No matching IF-THEN clause.
 
76 Output = Inventory ;
77 input @1 Date mmddyy10.
78 @11 Amount 5. ;
79 run ;
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.PROB21_10 may be incomplete. When this step was stopped there were 0 observations and 8 variables.
WARNING: Data set WORK.PROB21_10 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
80
81 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
94
 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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 ;

View solution in original post

16 REPLIES 16
ChrisNZ
Tourmaline | Level 20

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;

 

rvsidhu035
Quartz | Level 8

sas ready sales variable here

what value we get in sales variable

Kurt_Bremser
Super User
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 ;
ManitobaMoose
Quartz | Level 8

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:

 

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 Prob21_10 ;
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 Data mmddyy10. ;
81 end ;
82 run ;
 
NOTE: Variable Sales is uninitialized.
NOTE: Variable Inventory is uninitialized.
NOTE: Variable Data 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.PROB21_10 has 6 observations and 9 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.02 seconds
 
 
83
84 Data Mixed ;
85 Set Work.Prob21_10 ;
86 If Code eq 2 then Output Sales ;
_____
455
87 Else if Code ne 2 then Output Inventory ;
_________
455
ERROR 455-185: Data set was not specified on the DATA statement.
 
88 run ;
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.MIXED may be incomplete. When this step was stopped there were 0 observations and 9 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
 
 
89
90 Title 'Sales' ;
91 proc print data=Sales noobs ;
ERROR: File WORK.SALES.DATA does not exist.
92 run ;
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
93
94 Title 'Inventory' ;
 
 
95 proc print data=Inventory noobs ;
ERROR: File WORK.INVENTORY.DATA does not exist.
96 run ;
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
97
98 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
111
 

 

ChrisNZ
Tourmaline | Level 20

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 ;

ManitobaMoose
Quartz | Level 8

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/2005100
1.. ..11/15/2005200
2..A1368250...
2..B1111300...
1.. ..01/03/20055000
2..A887719...

Inventory

 Code Output Sales PartNumber Quantity Inventory Date Amount

1.. ..10/21/2005100
1.. ..11/15/2005200
2..A1368250...
2..B1111300...
1.. ..01/03/20055000
2..A887719...
ManitobaMoose
Quartz | Level 8

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!.

Kurt_Bremser
Super User

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.

ManitobaMoose
Quartz | Level 8

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.

 

 

 
ManitobaMoose
Quartz | Level 8
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
       
ManitobaMoose
Quartz | Level 8
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!
Kurt_Bremser
Super User

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 16 replies
  • 3854 views
  • 0 likes
  • 4 in conversation