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

 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)

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10

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;

View solution in original post

1 REPLY 1
ShiroAmada
Lapis Lazuli | Level 10

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;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 4113 views
  • 0 likes
  • 2 in conversation