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

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
Barite | Level 11

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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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