BookmarkSubscribeRSS Feed
Justin9
Obsidian | Level 7

Hi,

 

For the code below, please can someone explain the logic (highlighted in red text below)? I'm confused about why the same "score_result" is used for if/then and else?

 

proc sql;

       %if %sysfunc (exist(score_result))

                %then insert into score_result;

                %else create table score_result as;

       select * from test;

quit;

 

If I wanted to permanently save into a permanently library, with libref "ac", would the code be the options below, or something else?

 

Option 1

proc sql;

       %if %sysfunc (exist(score_result))

                %then insert into ac.score_result;

                %else create table ac.score_result as;

       select * from test;

quit;

 

Option 2

proc sql;

       %if %sysfunc (exist(score_result))

                %then insert into score_result;

                %else create table ac.score_result as;

       select * from test;

quit;

 

Option 3

proc sql;

       %if %sysfunc (exist(ac.score_result))

                %then insert into ac.score_result;

                %else create table ac.score_result as;

       select * from test;

quit;

 

 

2 REPLIES 2
art297
Opal | Level 21

I would think that you'd want an option 3:

Option 3

proc sql;
       %if %sysfunc (exist(ac.score_result))
                %then insert into ac.score_result;
                %else create table ac.score_result as;
       select * from test;
quit;

Art, CEO, AnalystFinder.com

 

Kurt_Bremser
Super User

This macro code:

%if %sysfunc (exist(score_result))
%then insert into score_result;
%else create table score_result as;

alternatively either creates the text

insert into score_result

or the text

create table score_result as

which then, after being fed to the SQL compiler, results in two variants of the SQL statement.

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