Post by K507I’m trying to make SAS SQL query without FROM clause. For example in
Oracle it works as: SELECT “cat” as animal FROM DUAL (it will return one
row with one column (animal) with a “cat” as a value). Is there anything
similar in SAS SQL? Of course I can use any table, but I need to find
more elegant solution. I appreciate any help
K507
As past Oracle PL/SQL programmer, using DUAL is handy as a simple calculator or a simple scenario/sql tester. As mentioned by Jack Hamilton SELECT “cat” as animal FROM DUAL does have a FROM. However, the original poster intended to state that the "result" doesn't come from a table.
In a default SAS installation with local tables, the following are possible to mimic what DUAL allows in Oracle SQL:
proc sql;
select 1 from sashelp.class (obs=1);
select 1 as number from sashelp.class (obs=1);
select '1' format=$12. from sashelp.class (obs=1);
select '1' as charx format=$12. from sashelp.class (obs=1);
select 9/(5*1024) from sashelp.class (obs=1);
select 9/(5*1024) format=dollar10.6 from sashelp.class (obs=1);
select put(9/(5*1024),dollar10.6) from sashelp.class (obs=1);
select 'cat' from sashelp.class (obs=1);;
select 'cat' as animal from sashelp.class (obs=1);
quit;
The "Dictionary" library is not visible, so you have to know the names of the tables before using them as a replacement for DUAL. So, for anyone who is a "noob" to SAS (a nod to 'noobs' in SAS, lol), any table in sashelp (such as sashelp.class is a good replacement for DUAL). The obs=1 in parentheses is required to ensure the select clause is only executed once. Omitting it will mean that the clause will execute once for each row within the table being used.
Creating a table called DUAL in the default work library is an alternative (whether by using SQL or by using SAS data step [the latter is easier than SQL]) but you need to ensure that any pseudo column name used within the select clause you try out does not match the column name used to create the DUAL.
e.g.
DATA DUAL;
_dummy_ = " ";
RUN;
(run this once per SAS session - column name is _dummy_ which is unlikely to be needed 'in anger' and the value is a SAS null [for a character column]).
Then you can use:
proc sql;
select 1 from DUAL;
quit;
(or whatever you need).
However, once your session is closed the DUAL table will be deleted, so you have to re-create it for every session in which you intend to use the DUAL.
So, if you have 'write' permissions to another library then you could create it there (so that it doesn't need to be re-created every time). But this means that DUAL would need to be prefixed by the relevant library name each time.
e.g.
DATA MYLIB.DUAL;
_dummy_ = " ";
RUN;
then in any future session you can use :
proc sql;
select 1 from MYLIB.DUAL;
quit;
until MYLIB.DUAL is deleted.