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

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

8 REPLIES 8
Imroze
Fluorite | Level 6

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

data_null__
Jade | Level 19

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_

Amir
PROC Star

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.

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

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

TomKari
Onyx | Level 15

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

sas_9
Obsidian | Level 7

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!!!

TomKari
Onyx | Level 15

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

sas_9
Obsidian | Level 7

thanks!!!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 2296 views
  • 0 likes
  • 6 in conversation