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

Im looking to put an IF Statement in my join, here is the code:

PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.*,
			    t2.Group
		FROM WORK.HAVE t1 INNER JOIN
			 WORK.GROUPS t2 ON (t1.UNIQ_ID = t2.UNIQ_ID AND t1.TYPE = t2.TYPE AND
					t2.PERIOD = IF(t1.TYPE = 'ABC', SUBSTR(t1.DATE_SK, 1, 6), SUBSTR(t1.DATE_SK, 1, 4)01));
QUIT;

But I keep getting the following error:

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

Any thoughts? Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

There is no IF() function of that nature in SAS. 

There are IFC()/IFN() functions and you can use those or use a CASE statement. 

 

I suspect that 01 beside the substring will also cause an issue?

Did you mean to add a CATT function to concatenate the '01' value to the substring value?

 

PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.*,
			    t2.Group
		FROM WORK.HAVE t1 INNER JOIN
			 WORK.GROUPS t2 ON (t1.UNIQ_ID = t2.UNIQ_ID AND t1.TYPE = t2.TYPE AND
					t2.PERIOD = IF(t1.TYPE = 'ABC', SUBSTR(t1.DATE_SK, 1, 6), SUBSTR(t1.DATE_SK, 1, 4)01));
QUIT;

 


@mhoward2 wrote:

Im looking to put an IF Statement in my join, here is the code:

PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.*,
			    t2.Group
		FROM WORK.HAVE t1 INNER JOIN
			 WORK.GROUPS t2 ON (t1.UNIQ_ID = t2.UNIQ_ID AND t1.TYPE = t2.TYPE AND
					t2.PERIOD = IF(t1.TYPE = 'ABC', SUBSTR(t1.DATE_SK, 1, 6), SUBSTR(t1.DATE_SK, 1, 4)01));
QUIT;

But I keep getting the following error:

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

Any thoughts? Thanks in advance!


 

View solution in original post

2 REPLIES 2
Reeza
Super User

There is no IF() function of that nature in SAS. 

There are IFC()/IFN() functions and you can use those or use a CASE statement. 

 

I suspect that 01 beside the substring will also cause an issue?

Did you mean to add a CATT function to concatenate the '01' value to the substring value?

 

PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.*,
			    t2.Group
		FROM WORK.HAVE t1 INNER JOIN
			 WORK.GROUPS t2 ON (t1.UNIQ_ID = t2.UNIQ_ID AND t1.TYPE = t2.TYPE AND
					t2.PERIOD = IF(t1.TYPE = 'ABC', SUBSTR(t1.DATE_SK, 1, 6), SUBSTR(t1.DATE_SK, 1, 4)01));
QUIT;

 


@mhoward2 wrote:

Im looking to put an IF Statement in my join, here is the code:

PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.*,
			    t2.Group
		FROM WORK.HAVE t1 INNER JOIN
			 WORK.GROUPS t2 ON (t1.UNIQ_ID = t2.UNIQ_ID AND t1.TYPE = t2.TYPE AND
					t2.PERIOD = IF(t1.TYPE = 'ABC', SUBSTR(t1.DATE_SK, 1, 6), SUBSTR(t1.DATE_SK, 1, 4)01));
QUIT;

But I keep getting the following error:

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

Any thoughts? Thanks in advance!


 

mhoward2
Obsidian | Level 7
Those two pieces solved it, thank you so much! Clearly I need to learn how to use functions in SAS better!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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