Use PROC SQL to create keep list for columns - variables

i need select colums whose names contains string '_fire'. Names can be obtained by statement select
proc sql;
create table x as select _name_
from work.x
where lowcase(_name_) like '%_fire'
;quit;
But i dont know, what to do next? I tried insert this data into variable, but then i get error: Invalid value for the KEEP option.
proc sql noprint;
select _name_
into :names
from work.x
where lowcase(_name_) like '%_fire';
quit;
DATA twowks1 ;
SET work.&tabulka. (KEEP = &names. ) ;
RUN;
can anyone help me? Thx

You're missing the separated by in your PROC SQL query.
However, I only get your error when &Names does not exist, so is there an error with your SQL Query?
*To generate your error;
%symdel names;
DATA twowks1;
SET work.&tabulka. (KEEP=&names.);
RUN;
Here is some that works as you're probably expecting.
*Works as expected;
proc sql noprint;
select _name_ into :names separated by " "
from work.x
where lowcase(_name_) like '%_fire';
quit;
DATA twowks1;
SET work.&tabulka. (KEEP=&names.);
RUN;

Related

Is there a way to nest a sql statement in a SAS IF/THEN/DO function?

So I have a SAS job that is scheduled to run and update a table regularly. I'm trying to add a functionality that drops the oldest month from the table when it is updating on the first day of a new month. Right now it looks like this:
PROC sql;
create table DropOldMonth as
select *
from ret.ServiceGFR_Impact;
create table DropOldMonth2 as
select *
from DropOldMonth
where date <> 'Jan 2020';
data _null_;
IF FirstDayofMonth = &todaysDate THEN DO;
proc sql;
drop table ret.ServiceGFR_Impact;
data ret.ServiceGFR_Impact;
set work.DropOldMonth2;
END;
run;
But I get this error:
ERROR 117-185: There was 1 unclosed DO block.
right below the Proc sql statement. I assume it's because there's a proc sql statement before and END statement to the DO function. However I need it to drop that table when that IF condition is true.
You may want to edit it in place instead of dropping it:
data ret.ServiceGFR_Impact;
set ret.ServiceGFR_Impact;
where date <> 'Jan 2020';
run;
make sure you have a backup before running it

SAS SQL create table depending on macro variable result

I am struggling with creating a new table in proc sql SAS depending on macro variable result.
1) I want to check if necessary table exists.
2) If it exists then I want to create a new table with given parameters.
3) If it doesn't exist I want to create a new table with different parameters.
I think I know how to check if table exists (0 or 1 in log results):
%let tex1 = %sysfunc(exist(Base.pk_&monthP1));
%put tex1 = &tex1.;
But I do not know how to implement this result into proc sql statement.
I need sth like this:
proc sql;
create table test as
select case when &text1 = 0 then select ...
else
select ...
end ;
quit;
Thank you in advance for suggested solutions.
So if both tables have the same structure then the only part of the SQL code that needs to change is the FROM clause. It is probably easier to conditionally set a macro variable to the name to use and replace the name of the dataset with a reference to the macro variable.
select var1,varb, ....
from &dsname.
Now the problem becomes one of just setting the macro variable. You could do that with macro logic. But you could also just do that with data step logic.
data _null_ ;
if exist("Base.pk_&monthP1") then call symputx('dsname','table1');
else call symputx('dsname','table2');
run;
proc sql;
... from &dsname. ...

Passing a value from Netezza back to SAS using a macro variable

we're using SAS 7.13 HF1 (7.100.3.5419) (64-bit)
I'm currently looking at a post that shows how to pass a value from SAS to a database that you're connecting in to. Here is the example below. You can see they take the Macro variable StartDate and pass it into Teradata to be used in the query.
%let StartDate = 2016/01/01;
proc sql;
connect to teradata
(BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.);
CREATE TABLE WorkTest.test AS
select * from connection to teradata
(
SELECT
TOP 10 *
FROM SomeCalendarData
WHERE SomeDate = %bquote('&StartDate.');
);
quit;
I want to go the other way.
How can I read a value from a similar query, only my DB is Netezza, and somehow pass it to a macro variable in SAS?
Thanks!
You would use the
SELECT <expression> INTO :<macro_var>'
statement. This is available in the PROC SQL query but not in the pass-through code, so it would look something like
proc sql;
connect to teradata
(BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.);
select somedate into :my_macro_var from connection to teradata
(
SELECT somedate
FROM SomeCalendarData
WHERE id = 101;
);
quit;
See the docs here: http://support.sas.com/documentation/cdl/en/sqlproc/63043/HTML/default/viewer.htm#n1tupenuhmu1j0n19d3curl9igt4.htm

how to append all tables with their table name in SAS

I'm trying to append all the tables(same structure) in the work library into one data set. But I need to have a new column name which can indicate the table name.
I tried two methods:
macro array and do over:
PROC APPEND BASE=_dupout DATA=dup_&dataset. FORCE;
RUN;
Proc SQL:
PROC SQL;
SELECT MEMNAME, catx('.', libname, MEMNAME) INTO : MEMNAMES SEPARATED BY ' '
from dictionary.tables
where libname='WORK';
quit;
DATA DUP_OUT;
SET &MEMNAMES.;
RUN;
but neither of those I can find a way add a new column (table name). Maybe it is a very simple question? I'm stuck..please help...
Very close, use the INDSNAME option in your SET statement.
DATA DUP_OUT;
SET &MEMNAMES. INDSNAME=SOURCE;
DSET=SOURCE;
RUN;

How to remove format and informat in proc sql

I googled and found that one can specify format in proc sql by
proc sql;
create table t1 as
select var1 length=20 label='mylabel' format=$20. informat=$20.
from db;
quit;
I know I can remove the format and informat by data step, but I want to know how to do it in proc sql.
Thanks!
I don't know how to handle it in SQL but you can use proc datasets instead without having to rewrite the dataset, just working at the metadata level:
proc datasets lib=libname memtype=data;
modify dsname;
attrib _all_ format=;
run;