BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to perform left join with a data set that is not existing.

I want to create a dynamic process that IF data set bbb  doesn't exist then I will not get an error .

I get an error

"

37 QUIT;
____
22
76
ERROR 22-322: Syntax error, expecting one of the following: ',', GROUP, HAVING, ORDER, WHERE.

ERROR 76-322: Syntax error, statement will be ignored."

 

 

data aaa;
input id x;
cards;
1 10
2 20
3 30
;
run;
 
%macro xxx;
PROC SQL;
	create table wanted  as
	select  a.*,b.Y
	from  aaa as a
	%if %sysfunc(exist(bbb)) %then
	left join bbb as b
	on a.id=b.id  
;
QUIT;
%mend xxx;
%xxx;
6 REPLIES 6
Kurt_Bremser
Super User

You are missing a semicolon (one is needed to terminate the %IF, the other for the SQL SELECT).

 

Since SAS 9.4M6, you do not need a macro definition anymore:

proc sql;
create table wanted  as
  select a.*, b.Y
  from aaa as a
%if %sysfunc(exist(bbb)) %then %do;
  left join bbb as b
  on a.id = b.id  
%end;
;
quit;
Ronein
Onyx | Level 15

Hello

I still  recieve an error   (I am using SAS enterprise  guide 7.1)

 

data aaa;
input id x;
cards;
1 10
2 20
3 30
;
run;


proc sql;
create table wanted  as
  select a.*, b.Y
  from aaa as a
%if %sysfunc(exist(bbb)) %then %do;
  left join bbb as b
  on a.id = b.id  
%end;
;
quit;
/*ERROR: The %IF statement is not valid in open code.*/


%macro RRR;
proc sql;
create table wanted  as
  select a.*, b.Y
  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: Unresolved reference to table/correlation name b.*/

 

 

Ronein
Onyx | Level 15

Thank you

Still got an error

___
22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, '.', /, <, <=, <>, =, >, >=, ?, AND, AS,
CONTAINS, EQ, EQT, FROM, GE, GET, GT, GTT, LE, LET, LIKE, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.

 

%macro RRR;
proc sql;
create table wanted  as
  select a.*,%if %sysfunc(exist(bbb)) %then %do; b.Y %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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1494 views
  • 0 likes
  • 2 in conversation