BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
fabiopjr
Fluorite | Level 6

I need to use a query in (3) when the number of records in table 1 is zero and another query when the result is greater than zero.

sas.jpg

I tried the code(inside 3) below but IF-THEN didn't work.

can anybody help me?

 

proc sql noprint;
select count(*) into :N from WORK.CAPS;
*/%put &N;
quit;


PROC SQL;
if &N = 0 then

   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
   SELECT t1.CPF, 
          t1.DSC_NOME_PESSOA_FISICA, 
          t1.PRIMEIRO_NOME
      FROM WORK.CAP5 t1

else
   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
   SELECT t2.CPF, 
          t2.DSC_NOME_PESSOA_FISICA, 
          t2.PRIMEIRO_NOME
      FROM WORK.CAPS t1
           INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);

QUIT;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

You must be on a older maintenance release that's prior to the introduction of macro %IF statements in open code.

Below code should work for you.

%macro doit();
  data _null_;
    call symputx('N',nobs,'l');
    stop;
    set work.caps nobs=nobs;
  run;

  PROC SQL;
    %if &N = 0 %then
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t1.CPF, 
            t1.DSC_NOME_PESSOA_FISICA, 
            t1.PRIMEIRO_NOME
          FROM WORK.CAP5 t1;
      %end;
    %else
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t2.CPF, 
            t2.DSC_NOME_PESSOA_FISICA, 
            t2.PRIMEIRO_NOME
          FROM WORK.CAPS t1
            INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
      %end;
  QUIT;
%mend;
%doit();

I've also replaced the select count(*) into logic with a SAS data _null_ step because with SAS tables the number of rows in the table is stored in the descriptor ("header") portion of the table.

A select count(*) needs to read all the data while the data _null_ step I've posted always only needs to read the descriptor portion of the table which is rather beneficial should you potentially have a lot of rows stored in the table.

View solution in original post

9 REPLIES 9
Amir
PROC Star

Hi,

 

Instead of using:

 

if &N = 0 then

 

Try using:

 

%if &N = 0 %then
%do;
  /* place code here to run if there are 0 obs */
%end;
%else
%do;
  /* place code here to run if there are one or more obs */
%end;

 

The %do; %end; and %else are required.

 

 

Thanks & kind regards,

Amir.

 

Edit: Add %else and formatting.

fabiopjr
Fluorite | Level 6

Hi, Amir.

Thanks for your response.

I tried this:.

proc sql noprint;
select count(*) into :N from WORK.CAPS;
*/%put &N;
quit;


PROC SQL;
%if &N = 0 %then
	%do;

	   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
	   SELECT t1.CPF, 
	          t1.DSC_NOME_PESSOA_FISICA, 
	          t1.PRIMEIRO_NOME
	      FROM WORK.CAP5 t1

	%end;

%else
	%do;
		CREATE TABLE WORK.QUERY_FOR_CAPS AS 
		SELECT t2.CPF, 
		      t2.DSC_NOME_PESSOA_FISICA, 
		      t2.PRIMEIRO_NOME
		  FROM WORK.CAPS t1
		       INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
	%end;

QUIT;

But I received the following error:

 

Captura de tela 2024-01-29 191121.png

 

SASKiwi
PROC Star

Since you are using an older version of SAS you will need to contain the macro statements inside a macro:

%macro SQL_Code;

PROC SQL;
%if &N = 0 %then
	%do;

	   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
	   SELECT t1.CPF, 
	          t1.DSC_NOME_PESSOA_FISICA, 
	          t1.PRIMEIRO_NOME
	      FROM WORK.CAP5 t1

	%end;

%else
	%do;
		CREATE TABLE WORK.QUERY_FOR_CAPS AS 
		SELECT t2.CPF, 
		      t2.DSC_NOME_PESSOA_FISICA, 
		      t2.PRIMEIRO_NOME
		  FROM WORK.CAPS t1
		       INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
	%end;

QUIT;

%mend SQL_Code;
%SQL_Code;
Patrick
Opal | Level 21

You must be on a older maintenance release that's prior to the introduction of macro %IF statements in open code.

Below code should work for you.

%macro doit();
  data _null_;
    call symputx('N',nobs,'l');
    stop;
    set work.caps nobs=nobs;
  run;

  PROC SQL;
    %if &N = 0 %then
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t1.CPF, 
            t1.DSC_NOME_PESSOA_FISICA, 
            t1.PRIMEIRO_NOME
          FROM WORK.CAP5 t1;
      %end;
    %else
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t2.CPF, 
            t2.DSC_NOME_PESSOA_FISICA, 
            t2.PRIMEIRO_NOME
          FROM WORK.CAPS t1
            INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
      %end;
  QUIT;
%mend;
%doit();

I've also replaced the select count(*) into logic with a SAS data _null_ step because with SAS tables the number of rows in the table is stored in the descriptor ("header") portion of the table.

A select count(*) needs to read all the data while the data _null_ step I've posted always only needs to read the descriptor portion of the table which is rather beneficial should you potentially have a lot of rows stored in the table.

fabiopjr
Fluorite | Level 6

Patrick or someone else,

Could you explain this piece of code to me?

 

  data _null_;
    call symputx('N',nobs,'l');
    stop;
    set work.caps nobs=nobs;
  run;
Tom
Super User Tom
Super User

Creates a local macro variable named N with the number of observations in the dataset named WORK.CAPS.

 

The first line starts a data step.  The _NULL_ means that no output dataset will be written.

 

The second line creates the macro variable.  The first argument is the name of the macro variable to create, the second is the value to put into the macro variable and the last says to make the variable as local, even if there already exists another macro variable in a different scope with that same name.

 

The third line ends the execution of the data step so that it only iterates once, no matter how many observations the input dataset has.

 

The fourth line says to read from WORK.CAPS and set the variable named NOBS to the number of observations that WORK.CAPS has.  The value of NOBS is set before the data step starts executing.

 

The last line ends the data step.

 

fabiopjr
Fluorite | Level 6
Thank you very much.
Tom
Super User Tom
Super User

This is why it helps to state the version of SAS you are using.  (The version of Enterprise Guide does not matter here.  Instead you need to tell us the version of SAS that you have connected to from Enterprise Guide.)

 

Also why are you using such an old version of SAS?  You still have to pay the license fees every year, you might as well use the latest version so you can use all of the new features.

Tom
Super User Tom
Super User

Why? 

When there are no observations in CAPS those two queries will generate the exact same results, an empty dataset with three variables.

 

So why not just always run the second one?

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 9 replies
  • 557 views
  • 3 likes
  • 5 in conversation