Hello,
I try to build a macro to sort a dataset by '&byvar.' if it is not empty set. Could anyone tell me whether it is OK to do proc sort within if statement? I have following codes and currently getting errors when I have empty set.
%macro m_sort(byvar=); proc sql; select count(*) into: obsn from df1; quit; %if &obsn. ^= 0 %then %do; proc sort data=df1; by &byvar.; run; %end; %mend;
PROC SORT has no trouble sorting any empty dataset. It can do that VERY fast.
PROC SQL has no trouble counting the number of observations from an empty dataset. That is also very fast.
Example:
214 %let obsn=unknown; 215 proc sql noprint; 216 select count(*) format=32. into :obsn trimmed from empty; 217 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 218 %put &=obsn ; OBSN=0 219 proc sort data=empty; 220 by name; 221 run; NOTE: Input data set is empty. NOTE: The data set WORK.EMPTY has 0 observations and 5 variables. NOTE: PROCEDURE SORT used (Total process time): real time 0.02 seconds cpu time 0.03 seconds
So the problem is probably something else.
Are you sure the dataset has the BY variable in it?
222 proc sort data=empty; 223 by fred; ERROR: Variable FRED not found. 224 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SORT used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Perhaps you should instead be testing if the by variable(s) exist in the dataset instead of worrying whether it is empty or not.
PROC SORT has no trouble sorting any empty dataset. It can do that VERY fast.
PROC SQL has no trouble counting the number of observations from an empty dataset. That is also very fast.
Example:
214 %let obsn=unknown; 215 proc sql noprint; 216 select count(*) format=32. into :obsn trimmed from empty; 217 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 218 %put &=obsn ; OBSN=0 219 proc sort data=empty; 220 by name; 221 run; NOTE: Input data set is empty. NOTE: The data set WORK.EMPTY has 0 observations and 5 variables. NOTE: PROCEDURE SORT used (Total process time): real time 0.02 seconds cpu time 0.03 seconds
So the problem is probably something else.
Are you sure the dataset has the BY variable in it?
222 proc sort data=empty; 223 by fred; ERROR: Variable FRED not found. 224 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SORT used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Perhaps you should instead be testing if the by variable(s) exist in the dataset instead of worrying whether it is empty or not.
Can you post the log you get from running the code, including the error message? And since this involves a macro, please turn on system option MPRINT.
It is fine to use a macro language %IF statement to control the generation of a code for a PROC SORT step. There is really no relationship between the macro language %IF statement and the non-macro language SAS code within an %IF %THEN %DO block.
I don't see anything in the macro that should cause an error.
That said, it's not a problem to sort an empty data set, so I don't think this macro should be necessary. If you run code like below, it runs fine:
*Create an empty (0 records) dataset ;
data empty ;
set sashelp.class ;
stop ;
run ;
proc sort data=empty ;
by name ;
run ;
If the PROC SORT by variable does not exist in the empty dataset, then the PROC SORT would throw an ERROR. But in SAS, it's pretty unusual to have a dataset with no variables (but it is possible).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.