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