DATA Step, Macro, Functions and more

Create a file if the Have file as less than a number of records

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Create a file if the Have file as less than a number of records

[ Edited ]

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;

Accepted Solutions
Solution
‎09-26-2017 04:01 PM
Super User
Posts: 9,547

Re: Create a file if the Have file as less than a number of records


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 9,547

Re: Create a file if the Have file as less than a number of records

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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 503

Re: Create a file if the Have file as less than a number of records

Posted in reply to KurtBremser

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

Solution
‎09-26-2017 04:01 PM
Super User
Posts: 9,547

Re: Create a file if the Have file as less than a number of records


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Trusted Advisor
Posts: 1,282

Re: Create a file if the Have file as less than a number of records

[ Edited ]

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);
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 120 views
  • 1 like
  • 3 in conversation