I want to store count of dataset in variable like below
%let Cnt ;
create table work.delaycheck as
select * from connection to oracle
(
SELECT PTNR_ID,CLNT_ID,REPORTING_DATE_KEY,NET_SALES
FROM FACT_TABLE
MINUS
SELECT PTNR_ID,CLNT_ID,REPORTING_DATE_KEY,NET_SALES
FROM HIST_FCT
);
I want to store count of this table in the variable Cnt like below
Set Cnt = (Select count(*) from work.delaycheck )
And Then
If(Cnt=0)
THEN
DO NOTHING
ELSE
execute(
Insert into Oracle_table
select * from work.delaycheck
) by oracle;
disconnect from oracle;
quit;
How can I acheive these steps? Thanks In advance!!
Why? Every dataset within the SAS system already has metadata associated with the dataset stored. You can access this metadata directly by using the sashelp.vtable (and if you want columns, sashelp.vcolumn) or via the SQL syntax of dictionary.tables. Therefore you a) do not need to code this yourself, and b) do not need to store the numeric in a text macro variable. So for instance:
set cnt=(select nobs from sashelp.vtable where libname="WORK" and memname="ABC");
Assuming your dataset is work.abc.
Why do you want to make the insert conditional? When the table is empty, inserting in into another table does exactly nothing, and takes very little time.
But if you need to know this for other reasons, PROC SQL assigns the macro variable SQLOBS, which contains the number of rows in the output from the last SQL statement. So you could use that like this:
%macro insert;
proc SQL;
/* code to create work table here */
%if &sqlobs>0 %then %do;
/* code to do something with table here */
%end;
quit;
%mend;
%insert;
Of course, you cannot use a SQL EXECUTE call to insert rows from a SAS table. You would either have to load the data to a (temporary) Oracle table, and the perform the insert. Alternatively, you can assign the Oracle schema as a SAS libname and use e.g. PROC APPEND to insert your data:
proc SQL;
connect to Oracle(<connnect options>);
create table work.delaycheck as select * from connection to Oracle
(<Oracle select code>);
quit;
libname outlib Oracle <connect options>;
proc append data=delaycheck base=outlib.Oracle_table;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.