i have 3 datasets , fruits, vegetables and diary dataset names. if datasets (fruits, vegetables,diary ) exists do condiftional statements
if datasets (fruits, vegetables ) exists do conditional statements
i have code as follows, but it running all the all conditions:
%macro checkds(dsn,dsn1,dsn2);
%if %sysfunc(exist(&dsn)) %then %do;
%if %sysfunc(exist(&dsn1)) %then %do;
%if %sysfunc(exist(&dsn2)) %then %do;
proc sql;
create table test..
quit;
.....
%end;
%end;
%end;
%else %do;
data _null_;
file print;
put #3 @10 "Data set &dsn. does not exist";
put #3 @10 "Data set &dsn1. does not exist";
put #3 @10 "Data set &dsn2. does not exist";
run;
%end;
%mend checkds;
%checkds(vegetables,Fruits,Dairy)
---For two datasets check i have code as below----
%macro checkds(dsn,dsn1);
%if %sysfunc(exist(&dsn)) %then %do;
%if %sysfunc(exist(&dsn1)) %then %do;
proc sql;
create table test..
quit;
.....
%end;
%end;
%else %do;
data _null_;
file print;
put #3 @10 "Data set &dsn. does not exist";
put #3 @10 "Data set &dsn1. does not exist";
run;
%end;
%mend checkds;
%checkds(vegetables,Fruits)
Try this....
%macro Test;
proc sql noprint;
create table MYWANTEDTABLES as
select memname,
case when memname='FRUITS' THEN 1
when memname='VEGETABLES' THEN 3
when memname='DAIRY' THEN 5 END
as table_tag,
sum(case when memname='FRUITS' THEN 1
when memname='VEGETABLES' THEN 3
when memname='DAIRY' THEN 5 END) AS TOTAL
from sashelp.vtable
where
libname='SASHELP' and upcase(memname) in ('FRUITS','VEGETABLES','DAIRY') and memtype='DATA'
order by calculated table_tag;
quit;
/* In the step above, i assigned a coded value whenever a table exist which when I sum-up will
give me an idea which table or tables exist.
When the value of total is:
TOTAL =1 MEANS only FRUITS table exist.
TOTAL =4 MEANS FRUITS AND VEGETABLES tables exist.
TOTAL =9 MEANS FRUITS, VEGETABLES and DAIRY tables exist.
TOTAL =6 MEANS FRUITS and DAIRY tables exist.
TOTAL =8 MEANS VEGETABLES and DAIRY tables exist.
*/
%let mycount=&sqlobs.;
%put There are &mycount. tables found.;
%if %eval(&mycount.=3) %then %do;
%put You can insert the appropriate steps here.;
%end;
/* When &MYCOUNT=
3: Means all 3 tables exist.
*/
proc sql noprint;
select max(TOTAL) into: mytotal from MYWANTEDTABLES;
quit;
%let mytotal=&mytotal.;
%put &Mytotal.;
/*
MYTOTAL =1 MEANS only FRUITS table exist.
MYTOTAL =4 MEANS FRUITS AND VEGETABLES tables exist.
MYTOTAL =9 MEANS FRUITS, VEGETABLES and DAIRY tables exist.
*/
/* Assuming you want to trigger additional steps when
fruits and vegetables only exist */
%if %eval(&mytotal.=4) %then %do;
%put Insert additional codes when fruits and veges tables exist.;
%end;
%mend;
%Test;
Try this....
%macro Test;
proc sql noprint;
create table MYWANTEDTABLES as
select memname,
case when memname='FRUITS' THEN 1
when memname='VEGETABLES' THEN 3
when memname='DAIRY' THEN 5 END
as table_tag,
sum(case when memname='FRUITS' THEN 1
when memname='VEGETABLES' THEN 3
when memname='DAIRY' THEN 5 END) AS TOTAL
from sashelp.vtable
where
libname='SASHELP' and upcase(memname) in ('FRUITS','VEGETABLES','DAIRY') and memtype='DATA'
order by calculated table_tag;
quit;
/* In the step above, i assigned a coded value whenever a table exist which when I sum-up will
give me an idea which table or tables exist.
When the value of total is:
TOTAL =1 MEANS only FRUITS table exist.
TOTAL =4 MEANS FRUITS AND VEGETABLES tables exist.
TOTAL =9 MEANS FRUITS, VEGETABLES and DAIRY tables exist.
TOTAL =6 MEANS FRUITS and DAIRY tables exist.
TOTAL =8 MEANS VEGETABLES and DAIRY tables exist.
*/
%let mycount=&sqlobs.;
%put There are &mycount. tables found.;
%if %eval(&mycount.=3) %then %do;
%put You can insert the appropriate steps here.;
%end;
/* When &MYCOUNT=
3: Means all 3 tables exist.
*/
proc sql noprint;
select max(TOTAL) into: mytotal from MYWANTEDTABLES;
quit;
%let mytotal=&mytotal.;
%put &Mytotal.;
/*
MYTOTAL =1 MEANS only FRUITS table exist.
MYTOTAL =4 MEANS FRUITS AND VEGETABLES tables exist.
MYTOTAL =9 MEANS FRUITS, VEGETABLES and DAIRY tables exist.
*/
/* Assuming you want to trigger additional steps when
fruits and vegetables only exist */
%if %eval(&mytotal.=4) %then %do;
%put Insert additional codes when fruits and veges tables exist.;
%end;
%mend;
%Test;
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.