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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 329 views
  • 0 likes
  • 3 in conversation