DATA Step, Macro, Functions and more

Output Sets When can you use them?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

Output Sets When can you use them?

What if you want to create several output sets from an original set? Proc means has the output out= option. So, I figured I would try the code below, as it should intuitively work. But, no dice. Why not, when can I use the output statement, and how could II, in this case, generate two data sets from the set learn.blood without creating separate data steps (i.e data subset_A ahd data subset_B)?

 

Libname Review'/folders/myfolders/Review' ;
Libname Learn'/folders/myfolders/Learn' ;  
Libname myformat'/folders/myfolders/sasuser.v94' ; 
Options fmtsearch=(myformat) ;

Data Review.Prob10_1 ; 
	Set Learn.Blood ; 
	Combined = (.001*WBC + RBC) ; 
	Where Gender = 'Female' AND BloodType = 'AB' ; 
		 Output Out = Subset_A ; 
	If Gender = 'Female' AND BloodType = 'AB' AND Combined ge 14 ; 
		 Output out = Subset_B ;
run ; 


Proc Print data=Subset_A noobs ; 
run ; 

Proc Print data=Subset_B noobs ; 
run ; 

The Log is Below:

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 61         
 62         Libname Review'/folders/myfolders/Review' ;
 NOTE: Libref REVIEW was successfully assigned as follows: 
       Engine:        V9 
       Physical Name: /folders/myfolders/Review
 63         Libname Learn'/folders/myfolders/Learn' ;
 NOTE: Libref LEARN refers to the same physical library as LEARN2.
 NOTE: Libref LEARN was successfully assigned as follows: 
       Engine:        V9 
       Physical Name: /folders/myfolders/Learn
 64         Libname myformat'/folders/myfolders/sasuser.v94' ;
 NOTE: Libref MYFORMAT refers to the same physical library as SASUSER.
 NOTE: Libref MYFORMAT was successfully assigned as follows: 
       Engine:        V9 
       Physical Name: /folders/myfolders/sasuser.v94
 65         Options fmtsearch=(myformat) ;
 66         
 67         Data Review.Prob10_1 ;
 68         Set Learn.Blood ;
 69         Combined = (.001*WBC + RBC) ;
 70         Where Gender = 'Female' AND BloodType = 'AB' ;
 71          Output Out = Subset_A ;
                          _
                          79
                      ___
                      455
 ERROR 79-322: Expecting a RC.
 
 ERROR 455-185: Data set was not specified on the DATA statement.
 
 72         If Gender = 'Female' AND BloodType = 'AB' AND Combined ge 14 ;
 73          Output out = Subset_B ;
                          _
                          79
                      ___
                      455
 ERROR 79-322: Expecting a RC.
 
 ERROR 455-185: Data set was not specified on the DATA statement.
 
 74         run ;
 
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set REVIEW.PROB10_1 may be incomplete.  When this step was stopped there were 0 observations and 8 variables.
 WARNING: Data set REVIEW.PROB10_1 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
       
 
 75         
 76         
 77         Proc Print data=Subset_A noobs ;
 ERROR: File WORK.SUBSET_A.DATA does not exist.
 78         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
       
 79         
 
 
 80         Proc Print data=Subset_B noobs ;
 ERROR: File WORK.SUBSET_B.DATA does not exist.
 81         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
       
 82         
 83         
 84         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 97         

Accepted Solutions
Solution
‎04-24-2018 02:58 PM
Regular Contributor
Posts: 237

Re: Output Sets When can you use them?

Posted in reply to tomrvincent

if you want it in one, here it is:


data Review.Subset_A Review.Subset_B ;
Set Learn.Blood ;
    Combined = (.001*WBC + RBC) ;
    if Gender = 'Female' AND BloodType = 'AB' then output Review.Subset_A;
    If Gender = 'Female' AND BloodType = 'AB' AND Combined ge 14  then output Review.Subset_B;
run;

View solution in original post


All Replies
Regular Contributor
Posts: 237

Re: Output Sets When can you use them?

Posted in reply to ManitobaMoose

I know we can output multiple sets in one data statement, but I prefer this way (I think it is clearer):

 

Data Review.Subset_A ; 
	Set Learn.Blood ; 
	Combined = (.001*WBC + RBC) ; 
	Where Gender = 'Female' AND BloodType = 'AB' ; 
run ;
Data Review.Subset_B ; 
	Set Learn.Blood ; 
	Combined = (.001*WBC + RBC) ; 
	If Gender = 'Female' AND BloodType = 'AB' AND Combined ge 14 ; 
run ; 
Proc Print data=Subset_A noobs ; run ; Proc Print data=Subset_B noobs ; run ;
Solution
‎04-24-2018 02:58 PM
Regular Contributor
Posts: 237

Re: Output Sets When can you use them?

Posted in reply to tomrvincent

if you want it in one, here it is:


data Review.Subset_A Review.Subset_B ;
Set Learn.Blood ;
    Combined = (.001*WBC + RBC) ;
    if Gender = 'Female' AND BloodType = 'AB' then output Review.Subset_A;
    If Gender = 'Female' AND BloodType = 'AB' AND Combined ge 14  then output Review.Subset_B;
run;

Frequent Contributor
Posts: 130

Re: Output Sets When can you use them?

Posted in reply to tomrvincent

Thanks for looking at this. I am aware of the method you have shown. I guess, at the core, I am trying to understand why the output statements are not generating data sets the way I have written them. When do output statements generate data sets, and when do they not? This is about understanding SAS more than about this specific example.

Super User
Posts: 13,942

Re: Output Sets When can you use them?

Posted in reply to ManitobaMoose

@ManitobaMoose wrote:

Thanks for looking at this. I am aware of the method you have shown. I guess, at the core, I am trying to understand why the output statements are not generating data sets the way I have written them. When do output statements generate data sets, and when do they not? This is about understanding SAS more than about this specific example.


Simple: Your syntax is incorrect for what you attempt/want to do.

Just like in any programming language I have to follow the syntax rules to do anything with code.

 

Your attempt to send output to data set without telling SAS that a dataset of that name is expected to be created is somewhat analogous to doing arithmetic with variables that have not been assigned a value or type. SAS will allow that though usually with a note in the log about variable x has not been initialized. Some program languages would consider that a critical fault and halt as you need to tell the program what type of variable it is before any use.

PROC Star
Posts: 1,472

Re: Output Sets When can you use them?

[ Edited ]
Posted in reply to ManitobaMoose

I think the point you are missing is that the OUTPUT statement in PROC MEANS (or any procedure) is a different statement than the OUTPUT statement in a DATA step.

 

The DATA step is really its own language.  PROCs are really their own things as well.  There are some statements, like WHERE which do the same thing in a DATA step and a PROC step.  But there are many statements which can only be used in the DATA step.  And some statements, like OUTPUT, are named the same but do different things in the DATA step and PROC step, and have different syntax.

 

The OUTPUT statement of PROC MEANS uses syntax like:

proc means data=sashelp.class ;
  output out=want ;
run ;

The OUTPUT statement of the DATA step uses syntax like:

data want ;
  set sashelp.class ;
  output want ;
run ;

They are two different statements, which happen to have the same name.

 

If you tried 'output want;' in PROC MEANS, it will be a syntax error.  If you tried 'output out=want;' in the DATA step, it's a syntax error. 

Regular Contributor
Posts: 237

Re: Output Sets When can you use them?

Posted in reply to ManitobaMoose

Because your data statement didn't refer to the 2 output datasets you wanted.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 121 views
  • 0 likes
  • 4 in conversation