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

Hello

I want to combine a list of data sets and it might happen that some of them doesnt exist.

What is the way to change the following statements  in order to add the condition ONLY IF EXISTS

 

 

%macro sset; 
%do j=1 %to &m.;
%let mon=%scan(&vector.,&j.,+);
LGD&mon.  /*How to add the condition only if data set exists*/
%end;
%mend sset;
%put %sset;

 

 

Data LGD2101;
Input ID  X;
CARDS;
1 10
2 20
3 30
;
Run;

Data LGD2102;
Input ID  X;
CARDS;
4 10
5 20
7 30
;
Run;

Data LGD2105;
Input ID  X;
CARDS;
8 60
9 40
;
Run;

%let vector=2101+2102+2103+2104+2105;
%let m=5;

%macro sset; 
%do j=1 %to &m.;
%let mon=%scan(&vector.,&j.,+);
LGD&mon.
%end;
%mend sset;
%put %sset;

data LGD_ALL;
SET  %sset_LGD;
Run;


data LGD_ALL;
SET  %sset;
Run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Isn't that what other macro does ?

Here is how I would code it:

%macro if_exist(prefix,list); 
%local j dsn;
%do j=1 %to %sysfunc(countw(&list,+));
  %let dsn=&prefix.%scan(&list.,&j.,+);
  %if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend if_exist;

%let vector=2101+2102+2103+2104+2105;
data wanted;
  set %if_exist(prefix=LGD,list=&vector) ;
run;

 

View solution in original post

8 REPLIES 8
ballardw
Super User

Build actual data set names.

Search for them in SASHELP.VTABLE or Dictionary.Tables. Remember Memname and Library have to be upper case.

PaigeMiller
Diamond | Level 26

We have already discussed how to determine if a SAS data set exists in your last thread.

--
Paige Miller
Ronein
Meteorite | Level 14
I want to learn another method ( different than SASHELP.VTABLE)
PaigeMiller
Diamond | Level 26

So you don't want to use a method that would work (SASHELP.VTABLE). Why?

 

Do you also want to not use the method we discussed yesterday? Why?

 

Typically, most people (including me) are happy to know two ways to accomplish a task, in fact one way is enough for me in most cases. 


If you have some particular constraints based upon your exact problem, please explain so we don't provide solutions that won't match the problem.

 

 

--
Paige Miller
Tom
Super User Tom
Super User

If you want to test if a dataset exists use the EXIST() function.

%macro sset; 
%local j dsn;
%if not %symexist(vector) %then %let vector=;
%do j=1 %to %sysfunc(countw(&vector,+));
  %let dsn=LGD%scan(&vector.,&j.,+);
  %if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend sset;
Ronein
Meteorite | Level 14

I am looking for a solution that is siamilar to this solution, 

But in my program it is a bit different because user should define a macro var called vector  that contrain dates (YYMM) that need to be set ....May anyone help to adjust the code to my task

%macro if_exists(varlist);
%if %sysfunc(exist(%scan(&varlist,1))) %then %scan(&varlist,1);
%mend;

data wanted;
SET
%if_exists(LGD2101)
%if_exists(LGD2102)
%if_exists(LGD2103)
%if_exists(LGD2104)
%if_exists(LGD2105);
run;

 

Tom
Super User Tom
Super User

Isn't that what other macro does ?

Here is how I would code it:

%macro if_exist(prefix,list); 
%local j dsn;
%do j=1 %to %sysfunc(countw(&list,+));
  %let dsn=&prefix.%scan(&list.,&j.,+);
  %if %sysfunc(exist(&dsn)) %then &dsn;
%end;
%mend if_exist;

%let vector=2101+2102+2103+2104+2105;
data wanted;
  set %if_exist(prefix=LGD,list=&vector) ;
run;

 

Ronein
Meteorite | Level 14
Great

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 747 views
  • 2 likes
  • 4 in conversation