BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

Hi Everyone,

I have a "have" file and it should have say 4 rows. 

I want to create a error file if the "have" file has less than 4row.

So in practice, when I see that error file, I know the issue.

Can anyone help me with that?

Thank you so much.

HHC

data have;
input var;
datalines;
1
2
3
;run;

 

 

data have;
input Jan1 _1_jan Feb March_ ;
datalines;
1 0 0 0
2 1 1 1
;run;

data _null_;
call symput('nobs',put(x,best.));
set have nobs=x;
stop;
run;

%macro errout;
%if &nobs le 4 %then %do;
data have;
set  have;
notice= "not enough observations";
run;
%end;
%mend;
%errout;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@hhchenfx wrote:

When I run your code, I receive this notice:

ERROR: Insufficient authorization to access C:\Program Files\SASHome\SASFoundation\9.3\errorout.
NOTE: The SAS System stopped processing this step because of errors.

 

I am using my computer and I am using Admin account.

 

Not sure why it is like that.

 

Thank you,


My filename was of course just a placeholder; use a name with an absolute path that points to a location where you have write permission.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

You can retrieve the number of observations in a dataset from sashelp.vtable or dictionary.tables (the latter with proc sql).

Or you can run a data step:

data _null_;
call symput('nobs',put(x,best.));
set have nobs=x;
stop;
run;

Then you can use that in a macro:

%macro errout;
%if &nobs le 4 %then %do;
data _null_;
file "errorout";
put "not enough observations";
run;
%end;
%mend;
%errout
hhchenfx
Rhodochrosite | Level 12

When I run your code, I receive this notice:

ERROR: Insufficient authorization to access C:\Program Files\SASHome\SASFoundation\9.3\errorout.
NOTE: The SAS System stopped processing this step because of errors.

 

I am using my computer and I am using Admin account.

 

Not sure why it is like that.

 

Thank you,

HC

Kurt_Bremser
Super User

@hhchenfx wrote:

When I run your code, I receive this notice:

ERROR: Insufficient authorization to access C:\Program Files\SASHome\SASFoundation\9.3\errorout.
NOTE: The SAS System stopped processing this step because of errors.

 

I am using my computer and I am using Admin account.

 

Not sure why it is like that.

 

Thank you,


My filename was of course just a placeholder; use a name with an absolute path that points to a location where you have write permission.

mkeintz
PROC Star

I think this is a good problem to wrap it all up in a macro using PROC SQL:

 

%macro test(lib=,dsn=,size=);
  %local dsn size lib;

  proc sql noprint;
    select nobs into :nrecs from dictionary.tables 
    where libname="%upcase(&lib)" and memname="%upcase(&DSN)";

  %if &nrecs < &size %then create table error as select * from &lib..&dsn;;
  quit;
%mend;
%test(lib=sashelp,dsn=class,size=20);
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1193 views
  • 1 like
  • 3 in conversation