Desktop productivity for business analysts and programmers

%sysfunc

Accepted Solution Solved
Reply
Regular Contributor
Posts: 220
Accepted Solution

%sysfunc

Hello friends - I need help on below logic please!!!

I am checking existance of file - if not exist than send email if exist check records and if records r less than certain number then send email...

%let testfile='c:\test.txt';

%macro check;

/*check file and if file exist take it into sas*/

%if %sysfunc(fileexist(&testfile) %then %do;

data work.abc;

infile 'c:\test.txt dlm=',' dsd;

input name $ id number1;

run;

%end;

/*if not exist then abort and send email*/

%else %do;

data _null_;

abort return;

run;

FIlename mymail email "emaild"

subject="sas message"

to="emailid";

data _null_;

file mymail;

put "file not exist";

run;

%end;

/*if exist then use sas dataset created by data step above*/

%else %do;

%let dsid=%sysfunc(open(abc));

%let num=%sysfunc(attrn(&dsid,nobs));

%let rc=%sysfunc(close(&dsid));

%put message;


%if &num<50000 %then %do;


FIlename mymail email "emaild"

subject="sas message"

to="emailid";


data _null_;

file mymail;

put "file has less than 50000 records";

run;

%end;


%else %if &num>50000 %then %do;

data _null_;

put 'enogh records';

run;


%mend;

%check;




/*but I am getting error about imbalance if then else statement*/


thanks!!!


Accepted Solutions
Solution
‎01-23-2014 07:26 AM
Respected Advisor
Posts: 3,786

Re: %sysfunc

You can do all of that in ONE data step.  Lookup FILEVAR option and PUT Statement E-Mail Directives

filename FT16F001 '~/willitfloat.dat';
%let obs=5000;
data _null_;
  
length message $128 filevar $256 to $64;
   to =
'you@yourhouse.com';
   filevar = pathname(
'FT16F001','F');
   rc = fileexist(filevar);
  
if rc eq 0 then do;
      Message =
'File does not exist';
     
link email;
      end;
  
else do;
     
infile dummy1 filevar=filevar eof=eof firstobs=&obs;
      input;
      message =
"File has at least &obs records";
     
link email;
    eof:
      message =
"File has less than &obs records";
     
link email;
      end;
    email:
     
file dummy2 email filevar=to;
      put '!em_subject! ' filevar=;
      put message=;
      put filevar=;
      stop;
  
run;
filename FT16F001 clear;

Message was edited by: data _null_

View solution in original post


All Replies
Contributor
Posts: 62

Re: %sysfunc

Please check wether %end statement should be specified before the %mend statement.

Solution
‎01-23-2014 07:26 AM
Respected Advisor
Posts: 3,786

Re: %sysfunc

You can do all of that in ONE data step.  Lookup FILEVAR option and PUT Statement E-Mail Directives

filename FT16F001 '~/willitfloat.dat';
%let obs=5000;
data _null_;
  
length message $128 filevar $256 to $64;
   to =
'you@yourhouse.com';
   filevar = pathname(
'FT16F001','F');
   rc = fileexist(filevar);
  
if rc eq 0 then do;
      Message =
'File does not exist';
     
link email;
      end;
  
else do;
     
infile dummy1 filevar=filevar eof=eof firstobs=&obs;
      input;
      message =
"File has at least &obs records";
     
link email;
    eof:
      message =
"File has less than &obs records";
     
link email;
      end;
    email:
     
file dummy2 email filevar=to;
      put '!em_subject! ' filevar=;
      put message=;
      put filevar=;
      stop;
  
run;
filename FT16F001 clear;

Message was edited by: data _null_

Super Contributor
Posts: 282

Re: %sysfunc

Hi,

Just based on the SAS log messages seen after submitting the macro definition and correcting the first two:

A close bracket is missing here:

%if %sysfunc(fileexist(&testfile)) %then %do;

A close quote is missing here:

infile 'c:\test.txt' dlm=',' dsd;

There is an %else %do that has no matching %if here:

/*if exist then use sas dataset created by data step above*/

%else %do;

Regards,

Amir.

Super Contributor
Super Contributor
Posts: 333

Re: %sysfunc

check this if its help - we just had same kind conversation before couple of days...

https://communities.sas.com/thread/52313

PROC Star
Posts: 1,146

Re: %sysfunc

Hi, Sandy

Although I assume you are using Enterprise Guide, for questions like this you should post them to one of the SAS programming forums; you'll get more SAS programmers looking at them there.

Tom

Regular Contributor
Posts: 220

Re: %sysfunc

Hi Tom - can i please have that link? - Thanks!!!

I think i resolve my issue...

i have created two separate macro logic

/*logic1*/

check if file exist and if exist then get into sas using data and infile statement

if not - then abort

/*if logic 1 is right then */

use that dataset created by data and infile statement from above and two separate condition

/*condition 1*/

if records are less than certain number then send email to team

/*condition 2*/

if records are good enough to process then right small message in log only - not sending any email to team.

- Thanks all!!!

PROC Star
Posts: 1,146

Re: %sysfunc

It's the same place you posted this. Just go up one level.

Two good ones for people programming in SAS are:

SAS Procedures

SAS Macro Facility, Data Step and SAS Language Elements

Tom

Regular Contributor
Posts: 220

Re: %sysfunc

thanks!!!

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 919 views
  • 0 likes
  • 6 in conversation