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.
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;
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.
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.
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:
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;
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.
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;
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.
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.
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?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.