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

Hi 

 

Im familiar to do an IF statement in Excel but not in SAS ( yet)

 

Im using proc sql and want to create a new table with the results. 

 

I have two tables and want to create a third one that says if the account number matches from table 1 with table 2 then add an another column in table 1 and say YES otherwise no. 

 

Finish this... 

Create table test1.new  as 

select A.* (table1) 

----case statement--- ( if account number column matches from table 1) then create new column and say YEs otherwise No, 

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

It makes no sense to say "as test.scrub B". Get rid of the A's and B's in your query, they aren't needed. Read about aliases in the SAS/SQL doc.

 

proc sql;
create table test.intscrub_1 as
select *, case when account_number in (select Acc_Num from test.scrub) then "YES" else "No" end as scrub
from test.intscrub;
quit;

Do not overwrite your input table until you have fully validated your query.

PG

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

SQL provides you with much more flexibility than Excel formulas. Once you master SQL basics, you will not want to go back. I suspect that you would want to do a lot more in a single query :

 

proc sql;
create table test1.new as
select 
	*, 
	case 
		when accountNb in (select accountNb from table2) then "YES"
		else "NO" end as inTable2
from table1;
quit;

(untested) 

PG
itshere
Obsidian | Level 7

Thank you but I get a Syntax Error

 

proc sql;
create table test.intscrub as
select A.*, case when A.account_number in (select B.Acc_Num from test.scrub B) then "YES" else "No" end as test.scrub B
from test.intscrub A
;quit;

 

any suggestions?

PGStats
Opal | Level 21

It makes no sense to say "as test.scrub B". Get rid of the A's and B's in your query, they aren't needed. Read about aliases in the SAS/SQL doc.

 

proc sql;
create table test.intscrub_1 as
select *, case when account_number in (select Acc_Num from test.scrub) then "YES" else "No" end as scrub
from test.intscrub;
quit;

Do not overwrite your input table until you have fully validated your query.

PG
Tom
Super User Tom
Super User

Why not learn SAS instead of SQL?

To combine two tables use the MERGE statement.  If you have other variables in TABLE2 that you do not want to carry over into the new table then use a KEEP= or DROP= dataset option.  Personally I find it easier to use 1/0 numeric variables than 'YES'/'NO' character variables.  Below is code to make both.

 

data want ;
   merge table1 (in=in1) table2(in=in2 keep=id);
   by id;
   if in1;
   new_numeric=in2;
   if in2 then new_character='YES';
   else new_character='NO ';
run;

   

PGStats
Opal | Level 21

@Tom wrote:

Why not learn SAS instead of SQL?

 

Because only SAS understands SAS? Smiley Happy

 

Seriously: if you are going invest in SAS, learn both! You'll need both to tackle DS2 later.

 

 

PG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 1123 views
  • 1 like
  • 3 in conversation