DATA Step, Macro, Functions and more

an easy question about "set by"statement

Reply
Contributor
Posts: 48

an easy question about "set by"statement

These codes work exactly I wanted

 

 

proc sort data=sashelp.class out=__temp; 
by age;
run;
data __temp1 ;
set __temp; 
by age ;

 

 

however,this get only five columns without any rows for the first time run.When I run it for the second time,it returned an error

 

data __temp2;
set sashelp.class;
by age;
run;

 

 ERROR: BY variables are not properly sorted on data set SASHELP.CLASS.
 Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 FIRST.Age=1 LAST.Age=1 _ERROR_=1 _N_=1
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: There were 2 observations read from the data set SASHELP.CLASS.
 WARNING: The data set WORK.__TEMP2 may be incomplete.  When this step was stopped there were 0 observations and 5 variables.
 WARNING: Data set WORK.__TEMP2 was not replaced because this step was stopped.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.01

 

although this is may be easy ,I found little about why and the document did not write anything about this.

 

someone knows please explain a little! thanks!

Super User
Posts: 2,078

Re: an easy question about "set by"statement

[ Edited ]
data __temp2;
set sashelp.class;
by age notsorted;
run;

age is not sorted in source sashelp.class whereas your __temp is sorted by age as you did using proc sort. 

Super User
Posts: 6,939

Re: an easy question about "set by"statement

When PROC SORT uses OUT=, it leaves the original data set intact.  The sorted version is stored (in this case) in __TEMP.  So the SET statement has to read:

 

set __temp;

 

Then you can use the BY statement without getting the error.  Don't forget the RUN statement at the end of the DATA step.

Ask a Question
Discussion stats
  • 2 replies
  • 55 views
  • 2 likes
  • 3 in conversation