<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can we use proc sort within if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943869#M369901</link>
    <description>&lt;P&gt;PROC SORT has no trouble sorting any empty dataset.&amp;nbsp; It can do that VERY fast.&lt;/P&gt;
&lt;P&gt;PROC SQL has no trouble counting the number of observations from an empty dataset.&amp;nbsp; That is also very fast.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;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 &amp;amp;=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
&lt;/PRE&gt;
&lt;P&gt;So the problem is probably something else.&lt;/P&gt;
&lt;P&gt;Are you sure the dataset has the BY variable in it?&lt;/P&gt;
&lt;PRE&gt;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

&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Perhaps you should instead be testing if the by variable(s) exist in the dataset instead of worrying whether it is empty or not.&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Sep 2024 20:55:17 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-09-13T20:55:17Z</dc:date>
    <item>
      <title>Can we use proc sort within if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943866#M369899</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to build a macro to sort a dataset by '&amp;amp;byvar.' if it is not empty set. Could anyone tell me whether it is OK to do proc sort within if statement?&amp;nbsp; I have following codes and currently getting errors when I have empty set.&lt;/P&gt;
&lt;PRE&gt;%macro m_sort(byvar=);
proc sql;
	select count(*) into: obsn from df1;
quit;
 
%if &amp;amp;obsn. ^=  0 %then %do;
	proc sort data=df1;
		by &amp;amp;byvar.;
	run;
%end;
%mend;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2024 20:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943866#M369899</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-09-13T20:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use proc sort within if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943869#M369901</link>
      <description>&lt;P&gt;PROC SORT has no trouble sorting any empty dataset.&amp;nbsp; It can do that VERY fast.&lt;/P&gt;
&lt;P&gt;PROC SQL has no trouble counting the number of observations from an empty dataset.&amp;nbsp; That is also very fast.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;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 &amp;amp;=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
&lt;/PRE&gt;
&lt;P&gt;So the problem is probably something else.&lt;/P&gt;
&lt;P&gt;Are you sure the dataset has the BY variable in it?&lt;/P&gt;
&lt;PRE&gt;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

&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Perhaps you should instead be testing if the by variable(s) exist in the dataset instead of worrying whether it is empty or not.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 20:55:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943869#M369901</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-13T20:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use proc sort within if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943870#M369902</link>
      <description>&lt;P&gt;Can you post the log you get from running the code, including the error message?&amp;nbsp; And since this involves a macro, please turn on system option MPRINT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is fine to use a macro language %IF statement to control the generation of a code for a PROC SORT step.&amp;nbsp; There is really no relationship between the macro language %IF statement and the non-macro language SAS code within an %IF %THEN %DO block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see anything in the macro that should cause an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, it's not a problem to sort an empty data set, so I don't think this macro should be necessary.&amp;nbsp; If you run code like below, it runs fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create an empty (0 records) dataset ;
data empty ;
  set sashelp.class ;
  stop ;
run ;

proc sort data=empty ;
  by name ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the PROC SORT by variable does not exist in the empty dataset, then the PROC SORT would throw an ERROR.&amp;nbsp; &amp;nbsp;But in SAS, it's pretty unusual to have a dataset with no variables (but it is possible).&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 20:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-proc-sort-within-if-statement/m-p/943870#M369902</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-09-13T20:57:40Z</dc:date>
    </item>
  </channel>
</rss>

