BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to perform left join via proc sql  and it might happen the the data set  is not existing.

In such case I want that the wanted data set will be created without fields from non existing data set (sex,revenue).

I get an error in this code,

may anyone help please?

ERROR: There is no matching %DO statement for the %END. This statement will be ignored.

 

 

Data AAA;
input ID X y ;
cards;
1 10 20
2 30 40
3 50 60
;
Run;


%macro RRR;
proc sql;
create table wanted as
select a.*,
        %if %sysfunc(exist(BBB)) %then %do; 
		b.revenue,
        b.Sex
		%end
from AAA as a
%if %sysfunc(exist(BBB)) %then %do;
left join BBB  as b
on a.ID=b.ID 
%end;
;
quit;
%mend RRR;
%RRR;
/*ERROR: There is no matching %DO statement for the %END. This statement will be ignored.*/

 

 

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

You just have to get the commas and semicolons right 🙂

 

Data AAA;
input ID X y ;
cards;
1 10 20
2 30 40
3 50 60
;
Run;


%macro RRR;
proc sql;
create table wanted as
select a.*
        %if %sysfunc(exist(BBB)) %then %do; 
        ,b.revenue
        ,b.Sex
      %end;
from AAA as a
%if %sysfunc(exist(BBB)) %then %do;
left join BBB  as b
on a.ID=b.ID 
%end;
;
quit;
%mend RRR;

options mprint;

%RRR
Reeza
Super User
For simplicity I'd code that differently - check if the data set exists and if it does do the join, if not then add the empty columns alone without a SQL query using base macro logic. Simpler code is easier to maintain.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 1042 views
  • 2 likes
  • 3 in conversation