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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;


View solution in original post

10 REPLIES 10
Ksharp
Super User
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;


Raj_C
Obsidian | Level 7

Can we give multiple datasets inplace of work.adverse ?

Ksharp
Super User

I don't understand what you mean.
If you want check multiple datasets, you can check dictionary table like :

select * from dictionary.members ;


terrencetang
Calcite | Level 5

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;

 

 

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why is it that you do not know what data you have?

Raj_C
Obsidian | Level 7
I mean to say if I am using multiple datasets I'm muy programs like adverse, conmeds, and ipdmin.
These I will define like this
DS 1 = adverse
DS 2 = conmeds
DS 3 = ipdmin

In this scenario how to proceed.

Here also if three datasets present then it should look for the code otherwise else if condition it should check.
Ksharp
Super User
CALL EXECUTE()


%macro xxx(dsn=,..........)
.......
%mend;


data _null_;
 input dsn : $40.;
 call execute('%xxx(dsn='||dsn||', .........  ) ');
cards;
adverse
conmeds
idpad
;
run;



Ksharp
Super User
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)

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Raj_C
Obsidian | Level 7
Thank you 🙂

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
  • 10 replies
  • 2151 views
  • 3 likes
  • 4 in conversation