Using proc sql commands in proc fcmp? - sql

I'm new to SAS and trying to create a user defined function that involves proc sql, the simplified version of the function is below;
proc fcmp outlib=work.funcs.test;
function calculate(table1, var1, motherTable);
proc sql noprint;
create table table1 as
select var1
from motherTable;
quit;
return();
endsub;
However, when I run the program I get the following:
ERROR: Subroutine 'calculate' was not terminated with ENDSUB.
ERROR: File WORK.MOTHERTABLE.DATA does not exist.
I am terminating the function with endsub(), and I know that motherTable doesn't exist because it's an argument to the function that hasn't been defined yet. Does anyone know what the problem could be? Thank you so much!

First, what you're doing is probably better done in a macro. That's how you do things like this most of the time in SAS.
%macro calc_func(in_table=, out_table=, var=);
proc sql noprint;
create table &out_table. as
select &var.
from &in_table.
;
quit;
%mend calc_func;
Second of all, you could do this in a user defined function (or a user defined call routine, more likely, as there's nothing being returned here); but you'd have to do it through a macro, if my understanding is right.
Check this paper for more information, or see the below example.
%macro calc_func();
%let table1=%sysfunc(dequote(&table1.));
%let var1=%sysfunc(dequote(&var1.));
%let motherTable=%sysfunc(dequote(&motherTable.));
%put _all_;
proc sql;
create table &table1. as (
select &var1.
from sashelp.&motherTable.)
;
quit;
%mend calc_func;
proc fcmp outlib=work.funcs.test;
function calculate(table1 $, var1 $, motherTable $);
rc = run_macro('calc_func', motherTable, table1, var1 );
return(rc);
endsub;
quit;
options cmplib=work.funcs;
data _null_;
x = calculate('newclass', 'age', 'class');
put x=;
run;
Basically, RUN_MACRO takes the macro name as an argument, and then allows FCMP to create macro variables with the names of the FCMP variables (or passed parameters). However, you have to remove their quotes, which is ... irritating. Good reason not to do this, unless it's truly necessary, I suppose.

The PROC SQL statement is ending the PROC FCMP compilation. You should just write that as a macro.
%macro calculate(table1, var1, motherTable);
proc sql noprint;
create table &table1 as
select &var1
from &motherTable
;
quit;
%mend calculate;

Related

Changing FROM statement with a variable

I am trying to change the name of the table I am getting my data from
Like this:
COREPOUT.KUNDE_REA_UDL_202112 --> COREPOUT.KUNDE_REA_UDL_202203
I create my variable like this:
PROC SQL NOPRINT;
SELECT DISTINCT
PERIOKVT_PREV_BANKSL_I_YYMMN6
INTO :PERIOKVT_PREV_BANKSL_I_YYMMN6
FROM Datostamp_PREV_Kvartal;
This is the code I want to use the variable for.
%_eg_conditional_dropds(WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000);
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000 AS
SELECT t1.Z_ORDINATE,
(input(t1.cpr_se,w.)) AS KundeNum
FROM COREPOUT.KUNDE_REA_UDL_202203 t1;
QUIT;
I have tried things like:
FROM string("COREPOUT.KUNDE_REA_UDL_",PERIOKVT_PREV_BANKSL_I_YYMMN6," t1";
I hope you can point me in the right direction.
Use & to reference and resolve macro variables into strings (e.g. &PERIOKVT_PREV_BANKSL_I_YYMMN6).
proc sql noprint;
select distinct PERIOKVT_PREV_BANKSL_I_YYMMN6
into :PERIOKVT_PREV_BANKSL_I_YYMMN6
from Datostamp_PREV_Kvartal
;
quit;
proc sql;
create table WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000 AS
select t1.Z_ORDINATE,
(input(t1.cpr_se,w.)) AS KundeNum
from &PERIOKVT_PREV_BANKSL_I_YYMMN6 t1
;
quit;
You can use CALL SYMPUTX() to move values from a dataset into a macro variable.
data _null_;
set Datostamp_PREV_Kvartal;
call symputx('dataset_name',PERIOKVT_PREV_BANKSL_I_YYMMN6);
stop;
run;
Then use the value of the macro variable to insert the dataset name into the code at the appropriate place. So your posted SQL is equivalent to this simple data step.
data QUERY_FOR_KUNDE_REA_UDL_20_0000;
set &dataset_name. ;
KundeNum = input(cpr_se,32.);
keep Z_ORDINATE KundeNum;
run;
Note: I did not see any definition of a user defined informat named W in your posted code so I just replaced it with the normal numeric informat instead since it looked like you where trying to convert a character value into a number.
The solution I ended up with was inspried by #Stu Sztukowski response:
I made a data step to concat the variable and created a macro variable.
data Concat_var;
str_PERIOKVT_PREV_YYMMN6 = CAT("COREPOUT.KUNDE_REA_UDL_",&PERIOKVT_PREV_BANKSL_I_YYMMN6," t1");
run;
PROC SQL NOPRINT;
SELECT DISTINCT
str_PERIOKVT_PREV_YYMMN6
INTO :str_PERIOKVT_PREV_YYMMN6
FROM Concat_var;
Then I used the variable in the FROM statement:
%_eg_conditional_dropds(WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000);
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000 AS
SELECT t1.Z_ORDINATE,
(input(t1.cpr_se,w.)) AS KundeNum
FROM &str_PERIOKVT_PREV_YYMMN6;
QUIT;
I hope this helps someone else in the future.

Using macro for formula proc sql in SAS

I need some help with macros in SAS. I want to sum variables (for example, from v_1 to v_7) to aggregate them, grouping by year. There are plenty of them, so I want to use macro. However, it doesn't work (I get only v_1) I would really appreciate Your help.
%macro my_macro();
%local i;
%do i = 1 %to 7;
proc sql;
create table my_table as select
year,
sum(v_&i.) as v_&i.
from my_table
group by year
;
quit;
%end;
%mend;
/* I don't know to run this macro - is it ok? */
data run_macro;
set my_table;
%my_macro();
run;
The macro processor just generates SAS code and then passes onto to SAS to run. You are calling a macro that generates a complete SAS step in the middle of your DATA step. So you are trying to run this code:
data run_macro;
set my_table;
proc sql;
create table my_table as select
year,
sum(v_1) as v_1
from my_table
group by year
;
quit;
proc sql;
create table my_table as select
year,
sum(v_1) as v_1
from my_table
group by year
;
quit;
...
So first you make a copy of MY_TABLE as RUN_MACRO. Then you overwrite MY_TABLE with a collapsed version of MY_TABLE that has just two variables and only one observations per year. Then you try to collapse it again but are referencing a variable named V_2 that no longer exists.
If you simply move the %DO loop inside the generation of the SQL statement it should work. Also don't overwrite your input dataset. Here is version of the macro will create a new dataset name MY_NEW_TABLE with 8 variables from the existing dataset named MY_TABLE.
%macro my_macro();
%local i;
proc sql;
create table my_NEW_table as
select year
%do i = 1 %to 7;
, sum(v_&i.) as v_&i.
%end;
from my_table
group by year
;
quit;
%mend;
%my_macro;
Note if this is all you are doing then just use PROC SUMMARY. With regular SAS code instead of SQL code you can use variable lists like v_1-v_7. So there is no need for code generation.
proc summary nway data=my_table ;
class year ;
var v_1 - v_7;
output out=my_NEW_table sum=;
run;

ORA-00928: missing SELECT keyword, SQL statement was not passed to the DBMS, SAS will do the processing

I am so frustrated with this piece of code. I'm trying to pass in values using syspbuff which I do all the time. However, I want to pass in multiple values but for this UNION code I'm trying to do, it's giving me trouble. I am going from Oracle to SAS which I assume is causing the problem but I'd like an answer as to why. Previously, I had the source tables in temp space (SAS) and I didn't get this error. But when I had to create the tables in MYDB (Oracle) because of a specific reason, I started getting the large log with "failure to pass through" errors.
Interestingly, the code actually works and it does what I want it to but the problem is that I get a pop up that the log is too large and will open externally. Then it opens a text file that is HUGE and has tons of errors basically saying that it couldn't pass through the code into implicit pass through. I wasn't trying to do pass through for this particular piece of code. So, again, it works and I ultimately get what I want but the log issue is driving me bonkers.
%macro ALLPROVTYPE() / parmbuff;
%do ii = 1 %to %sysfunc(countw(%bquote(&syspbuff.)));
%let FT=%scan(%bquote(&SYSPBUFF),&ii);
CREATE TABLE MYSASLIB.ALLST_PROV_&FT._NULL AS
SELECT "AK" AS STATE,*
FROM MYDB.AK_PROV_&FT
%macro JNSTS() / parmbuff;
%do i = 1 %to %sysfunc(countw(%bquote(&syspbuff.)));
%let ST=%scan(%bquote(&SYSPBUFF),&i);
UNION CORR
SELECT "&ST" AS STATE,*
FROM MYDB.&ST._PROV_&FT
%end;
%mend JNSTS;
%JNSTS(&&PROVALL&FT);
;
%end;
%mend ALLPROVTYPE;
PROC SQL;
%ALLPROVTYPE(&PROVNUMS);
QUIT;
ACCESS ENGINE: ERROR: ORACLE prepare error: ORA-00928: missing SELECT keyword. SQL statement: DEBUG: DBMS engine returned an error - NO Implicit Passthru.
DEBUG: Error during prepare of:
The way I understood this query is, you are creating multiple tables, and each table is created as a select statement which is constructed through multiple select statements that are joined via a UNION CORR. Essentially something like:
create table <something> as
(select <something> as state, * from <something> union corr
select <something> as state, * from <something> union corr
select <something> as state, * from <something>);
Is this correct?
If yes, your macro code had some syntactically problematic nesting going on. Try the following code (though I wasn't able to fully verify it since I don't have information about the inputs to the macros):
/* Since this needs to be passed between the two macros */
%global FT;
%macro ALLPROVTYPE() / parmbuff;
%do ii = 1 %to %sysfunc(countw(%bquote(&syspbuff.)));
%let FT=%scan(%bquote(&SYSPBUFF),&ii);
CREATE TABLE MYSASLIB.ALLST_PROV_&FT._NULL AS (
%JNSTS(&&PROVALL&FT)
);
%end;
%mend;
%macro JNSTS() / parmbuff;
%do jj = 1 %to %sysfunc(countw(%bquote(&syspbuff.)));
%let ST=%scan(%bquote(&SYSPBUFF),&jj);
SELECT "&ST" AS STATE,* FROM MYDB.&ST._PROV_&FT
%if &jj NE %sysfunc(countw(%bquote(&syspbuff.))) %then
%do;
UNION CORR
%end;
%end;
%mend;
PROC SQL;
%ALLPROVTYPE(&PROVNUMS);
QUIT;

SAS: How do I transfer variables between IML statements?

Let's say I'm trying to do the following:
%macro test(a=);
%do i=1 %to &a;
proc iml;
b=b//(2*i);
quit;
%end;
proc iml;
print sum(b);
quit;
%mend;
%test(a=2);
In the code I'm trying to write, I can't put it all in one IML (I need a proc freq within the do loop). The code above gives the error "Matrix b not set to a value." How do I tell SAS what b is so that I can still access it after I've quit the iml statement?
Two suggestions:
1) Use the STORE statement to write the matrix B to disk at the end of the first call, then use the LOAD statement to read it in during the second call:
store B;
quit;
proc freq data=...;
run;
proc iml;
load B;
...
2) An alternative approach is to call PROC FREQ from within your PROC IML program by using the SUBMIT and ENDSUBMIT statements:
/* compute B */
submit;
proc freq data=...;
run;
endsubmit;
s = sum(b): /* B is still in scope */
You need to rework things so the PROC IML; and QUIT; are outside of the macro. This is good practice most of the time even in other scenarios where it's not that important, but here it's necessary.
IE
%macro test(a=);
%do i=1 %to &a;
b=b//(2*i);
%end;
proc iml;
%test(a=5);
quit;
QUIT ends the PROC IML session and clears its memory.

Can this run in a Macro?

I have been trying to resolve an issue and using a different approach to resolve it. I have created a macro to get actual value. The sql generates an HTML view with the values I need.
%macro actualvalue();
proc sql noprint;
%do i=1 %to %wordcount(&fieldlist);
Select %scan(&fieldlist,&i) into :actualvar separated by ' ' FROM TableA Where
IncidentItemId=%scan(&incidentitemlist,&i);
%end;
quit;
%mend actualvalue;
However, the actualvar macro variable does not seem to capture the value. Is there something wrong in the way I am trying to initialize the macro variable or this cannot be performed inside a macro. Any thoughts on this would be appreciated.
I think each time your do loop runs it is overwriting the previous value of actualvar. You need to use something like
select %scan(&fieldlist,&i) into :actualvar&i ...
Then afterwards print out values for &actualvar1 &actualvar2 etc... to check your results.
At least you need to put PROC SQL statement inside the DO loop, since your aim is running proc sql for multiple times
%do i=1 %to %wordcount(&fieldlist);
proc sql noprint;
Select %scan(&fieldlist,&i) into :actualvar separated by ' ' FROM TableA
Where IncidentItemId=%scan(&incidentitemlist,&i);
%end;
I don't see any problem regarding the rest though. Give it a test and report any error.
I will modify this answer accordingly.