How to change a date formatted number into number in SQL - sql

I meet a very confusing problem when code SQL in SAS. My code is:
proc sql noprint;
select VDte into :vdate
from test1;
quit;
proc sql;
create table test3 as
select *, cdate>=&vdate. as index
from test2;
quit;
I find all index=1. There should be some index=0 and some index=1. When I use a number instead of macro variable vdate, eg. 17685(02Jun2008) instead of &vdate., it works!
I also checked the VDte. Its type is numeric, format is ddmmyy10.. That is to say VDte is a number stored in SAS! But when give it to &vdate., there is some problem!!
Could someone help me to understand this situation?
Thanks,
Andrea

If your VDte has a SAS date format, you need to "clear" it before storing its value to the macro variable:
proc sql;
select VDte format=8.
into :vdate
from test1;
quit;
Then your comparison should work fine.
Note that you could also use the date9. format for creating your macro variable and then use cdate>="&vdate"d in your second query.

Related

How to create multiple macro variables using a loop in SAS Enterprise Guide?

I am constantly using SAS datasets in SAS EG to create macro variables that can be used as variables in a query from SAS EG to my internal servers. There is a character limit of 65,534 for a macro variable. When I need to get 100k ids that are 9 to 15 digits in length the number of macro variables required to create really adds up. I am asking the community if there is a way to create a large number of macro variables with a loop instead of doing it manually.
For instance the manual way to create these macro variables would be something like the following:
proc sql; create table alerts as select distinct review_id format best12. from q4_21_alerts order by review_id;quit;
proc sql; create table alerts1 as select review_id, monotonic() as number from alerts order by number; quit;
proc sql; select distinct review_id into:alert_ids1 separated by ',' from alerts1 where number between 1 and 7000; quit;
proc sql; select distinct review_id into:alert_ids2 separated by ',' from alerts1 where number between 7001 and 14000; quit;
proc sql; select distinct review_id into:alert_ids3 separated by ',' from alerts1 where number between 14001 and 21000; quit;
proc sql; select distinct review_id into:alert_ids4 separated by ',' from alerts1 where number between 21001 and 27000; quit;
.
.
.
proc sql; select distinct review_id into:alert_ids21 separated by ',' from alerts1 where number between 140001 and 147000; quit;
I am hoping to find a way to do something like the following:
N = 145417
#total number of review_ids that need to be contained in SAS macro variables
L = 8
#length/number of characters/digits in each review_id
L = L + 1
#length/number of characters/digits in each review_id with 1 added for the comma separation in macro variable
stop = N*L
i = 1
while(i<=stop){
some code to create all 21 macro variables
}
then be left with macro variables alert_ids1, alert_ids2,...,alert_ids21 that would contain all 145,417 ids i need to then use in a query for my internal servers.
Any help would be appreciated!
I've used google and sas communities and have code to do this process manually...
I am unsure what your final query is and would advise building a SQL query that specifically filters to the IDs you want. e.g.:
proc sql;
create table want as
select *
from have
where id in(select id from id_table)
;
quit;
But if you need to have a comma-separated list of macro variables that abides by the 65,534 character length, the safest way is to create one ID per macro variable. You can very easily do this with a data step.
data _null_;
set alerts1;
call symputx(cats('alert_id', _N_), review_id);
call symputx('n_ids', _N_);
run;
This will create the macro variables:
alert_id1
alert_id2
alert_id3
...
Now you need to create a loop that makes these all comma-separated.
%macro id_loop;
%do i = 1 %to &n_ids;
&&alert_id&i %if(&i < &n_ids) %then %do;,%end;
%end;
%mend;
Note the code format is a bit strange to keep the output formatted correctly. Now run this macro and you'll see a comma-separated list of every alert ID:
%put %id_loop;
id1, id2, id3, ...
You can put this in a query, such as where alert_id in (%id_loop). Keep in mind that doing this will load up the symbol table with a ton of macro variables. This is not the recommended way to query, but it is one way to achieve what you asked.
Use a data step instead of SQL to create the macro variables.
You can even create a second macro variable that references all of the generated macro variables.
For example say you have determined that you can always fit 1000 values into a single variable (the limit for a data step variable is 32K instead of the 64K limit of a macro variable) then you could use a data step like this:
data _null_;
length string list $32767 ;
group+1;
do i=1 to 1000 until(eof);
set alerts end=eof;
string=catx(',',string,review_id);
end;
call symputx(cats('alert_id',group),string);
list = catx(',',list,cats('&alert_id',group'));
if eof then call symputx('alerts',list);
run;
Now you can use that single macro variable ALERTS that consists of the string
&alert_id1,&alert_id2,....
in your SQL code:
where review_id in (&alerts)
And filter on all of the values in the ALERTS dataset even if the total string is longer than 64K. Since you put 1000 into each macro variable and you can fit about 3000 references to those macro variables into the ALERTS macro variable you could store up to 3 million values.
Of course you might hit a limit on what the SQL processor can handle.

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.

Dynamize range of SAS PROC SQL SELECT INTO macro creation

I want to put multiple observations into an own macro variable. I would do this by using select into :obs1 - :obs4, however, as count of observations can differ, i would like to dynamize the range and my code looks like this:
proc sql;
create table segments as select distinct substr(name,1,6) as segment from dictionary.columns
where libname = 'WORK' and memname = 'ALL_CCFS' and name ne 'MONTH';
run;
proc sql noprint;
select count(*) into: count from segments;
run;
proc sql noprint;
select segment into :segment_1 - :segment_&count. from dictionary.columns;
run;
However, this doesn't seem to work... any suggestions? Thank you!
Leave last value empty/blank and SAS will create them automatically
Set it to an absurdly large number and SAS will only use what's required
Use a data step to create it where you can dynamically increment your number (not shown).
proc sql noprint;
select segment into :segment_1 -
from dictionary.columns;
run;
proc sql noprint;
select segment into :segment_1 - :segment_999
from dictionary.columns;
run;

SQL Accessing SAS Variable in Query

I'm attempting to use SASDOS in my statement below, but it's failing to be found. My understanding is that I have to use a form of derived table to access this new column. Is this correct? If so, could someone please help elaborate on how to do that?
proc sql;
create table TEST as
select
DQBBDA AS 'Sbm Date'n,
case when 'Sbm Date'n > 999999
then input('1' || substr(put('Sbm Date'n,z8.),3), z7.)
end as SASDOS format=z7.
from
DB2SCHEMA.ORIGIN
where
SASDOS = 1130314;
quit;
As sasfrog commented, you need to add the CALCULATED keyword to refer to a new column in SAS SQL and you should refer to the native DB2 column in your query. For example:
proc sql;
create table TEST as
select DQBBDA AS 'Sbm Date'n
, case when DQBBDA > 999999
then input('1' || substr(put(DQBBDA,z8.),3), z7.)
end as SASDOS format=z7.
from DB2SCHEMA.ORIGIN
WHERE CALCULATED SASDOS = 1130314;
quit;
However, you really should rethink what you are doing and figure out how to write a WHERE clause that uses only columns from DB2; otherwise the entire table must be pulled back to SAS (a likely poor solution). Cases like this are probably better solved using a pass-thru query (where you can execute native SQL directly in DB2).
UPDATE: Here is another (tested) example using a SAS data set rather than a table from a LIBNAME reference. Notice I'm also correcting a syntax error with the input function (the last parameter should be 7. not z7.).
data ORIGIN;
DQBBDA = 11130314; output;
DQBBDA = 22130314; output;
run;
options validvarname=any;
proc sql;
create table TEST as
select DQBBDA AS 'Sbm Date'n
, case when DQBBDA > 999999
then input('1' || substr(put(DQBBDA,z8.),3), 7.)
end as SASDOS format=z7.
from ORIGIN
WHERE CALCULATED SASDOS = 1130314;
quit;
I think rather than where calculated sasdos you should use having sasdos

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.