Hi SAS Users,
good morning!!!
libname raju 'C:\Users\q807540\Documents\SAS';
ds=adverse;
data adv;
set raju.&ds;
where sex = 'M';
run;
now
if the adverse SAS dataset present in the mentioned path then excel output should open with the output.(I am writing proc export step in the final.)
if the adverse dataset not present in the mentioned path then excel output should open saying "NO SAS DATASET PRESENT".
Thanks in advance.
Kind Regards,
Raju
You could write a macro. %if %sysfunc(exist(work.adverse)) %then %do; data ad; set work.adverse; run; %else %do; data ad; msg="Table not found." run; %end;
You could write a macro. %if %sysfunc(exist(work.adverse)) %then %do; data ad; set work.adverse; run; %else %do; data ad; msg="Table not found." run; %end;
Can we give multiple datasets inplace of work.adverse ?
I don't understand what you mean. If you want check multiple datasets, you can check dictionary table like : select * from dictionary.members ;
You can use MACRO to achieve it;
libname raju 'C:\Users\q807540\Documents\SAS';
%let ds=adverse; <I found that you are omitting the "%let">
%MACRO test;
%if %sysfunc(exist(raju.&ds)) %then %do;
data ad;
set raju.&ds;
where sex = 'M';
run;
%end;
%else %do;
data ad;
msg="NO SAS DATASET PRESENT";
run;
%end;
%MEND test;
%Test;
--------------------------------------------
You can also use %put function to make your message displaying in the SAS log instead of in a dataset.
%put NO SAS DATASET PRESENT;
Just replace the code between %else %do; and %end;
Why is it that you do not know what data you have?
CALL EXECUTE() %macro xxx(dsn=,..........) ....... %mend; data _null_; input dsn : $40.; call execute('%xxx(dsn='||dsn||', ......... ) '); cards; adverse conmeds idpad ; run;
You mean check the three tables at the same time ? %macro xx(library=sashelp,dsn=air cars class); %local n m; %let m=%sysfunc(countw(&dsn)); %let _dsn="%sysfunc(prxchange(s/\s+/" "/,-1,&dsn))"; proc sql noprint; select count(*) into : n from dictionary.members where libname=upcase("&library") and memname in (%upcase(&_dsn)); quit; %if &n=&m %then %put WARNING: Yes. ; %else %put WARNING: No. ; %mend; %xx(library=sashelp,dsn=air cars c) %xx(library=sashelp,dsn=air cars class)
Well, if you don't know what data you have, then your going to have an uphill struggle anyways. But to check this, why bother with all the macro nonsense making harder to read/maintain/debug code when you can simply:
data _null_; call symputx("adverse",exist(work.adverse)); call symputx("conmeds",exist(work.conmeds)); call symputx("ipdmin",exist(work.ipdmin)); run;
Or just execute your code conditionally:
data _null_; set sashelp.vtable (where=(libname="WORK" and memname in ("ADVERSE","CONMEDS","IPDMIN"))); if memname="ADVERSE" then call execute('%adverse_macro();'); if memname="CONMEDS" then call execute('%conmeds_macro();'); if memname="IPDMIN" then call execute('%ipdmin_macro();'); run;
Or, far better to fix the problem at source, i.e create three empty datasets called adverse, conmeds, ipdmin, and then append any data you get to them. The data always exists then even if there are no observations.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.