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

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         
1 ACCEPTED SOLUTION

Accepted Solutions
tomrvincent
Rhodochrosite | Level 12

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

6 REPLIES 6
tomrvincent
Rhodochrosite | Level 12

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 ;
tomrvincent
Rhodochrosite | Level 12

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;

ManitobaMoose
Quartz | Level 8

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.

ballardw
Super User

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

Quentin
Super User

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. 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
tomrvincent
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 6426 views
  • 0 likes
  • 4 in conversation