DATA Step, Macro, Functions and more

reading the sas dataset from the given path

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

reading the sas dataset from the given path

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


Accepted Solutions
Solution
‎10-04-2016 09:13 PM
Super User
Posts: 10,028

Re: reading the sas dataset from the given path

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


All Replies
Solution
‎10-04-2016 09:13 PM
Super User
Posts: 10,028

Re: reading the sas dataset from the given path

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;


Contributor
Posts: 31

Re: reading the sas dataset from the given path

Can we give multiple datasets inplace of work.adverse ?

Super User
Posts: 10,028

Re: reading the sas dataset from the given path


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

select * from dictionary.members ;


New Contributor
Posts: 2

Re: reading the sas dataset from the given path

[ Edited ]

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;

 

 

 

 

 

Super User
Super User
Posts: 7,955

Re: reading the sas dataset from the given path

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

Contributor
Posts: 31

Re: reading the sas dataset from the given path

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.
Super User
Posts: 10,028

Re: reading the sas dataset from the given path

CALL EXECUTE()


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


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



Super User
Posts: 10,028

Re: reading the sas dataset from the given path

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)

Super User
Super User
Posts: 7,955

Re: reading the sas dataset from the given path

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.

 

Contributor
Posts: 31

Re: reading the sas dataset from the given path

Thank you Smiley Happy
☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 455 views
  • 3 likes
  • 4 in conversation