BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

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

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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.

Quentin
Super User

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

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 364 views
  • 0 likes
  • 3 in conversation