How to use DATETIME in WHERE clause in SAS PROC SQL - sas

I have this data in a SAS table: 01Sep2016:21:31:27
I want to do this:
PROC SQL;
UPDATE lib1.tablename1
set Valid_From = '2000-01-01 00:00:00'dt
WHERE Valid_From = '1Sep2016:21:31:26'dt;
QUIT;
But the WHERE clause doesn't match. What is the correct format for the datetime value?

You were very nearly there - try this:
PROC SQL;
UPDATE lib1.tablename1
set Valid_From = '01jan2000:00:00:00'dt
WHERE Valid_From = '01Sep2016:21:31:26'dt;
QUIT;

Related

Merging multiple datasets together

Hello I have a listing I'm struggling with as I don't think the code I am using is doing the job correctly. Here is the spec.
Dataset: Firstly, merge QS with SUPPQS by USUBJID, IDVARVAL= QSSEQ, keep only records where QSCAT=’SOFA’. Then merge with ADSOFA by USUBJID and QSSEQ. Only keep records where MITTFL=’Y’
and here is the code I'm using
proc sql;
create table qs (where=(qscat="SOFA" )) as
select a.*,b.qnam as SOFASCS,qval as avalc_qs from trans.qs as a
left join
trans.suppqs (where=(qnam='SOFASCS')) as b
on a.usubjid = b.usubjid and a.qsseq = input(b.idvarval,best.);
quit;
proc sort data=qs;
by usubjid qsseq;
run;
data adsofa;
set adb.adsofa;
run;
proc sort data=adsofa;
by usubjid qsseq;
run;
data qs01;
merge qs(in=a drop=studyid)
adsofa(in=b where=(mittfl = "Y"));
by usubjid qsseq;
if a or b;
I keep getting rows I don't want. Is there a cleaner way of doing this?.
I tried to convert your logic into a classical SQL.
proc sql;
create table qs as
select a.*
,b.qnam as SOFASCS
,qval as avalc_qs
from trans.qs as a
left join trans.suppqs as b
on a.usubjid = b.usubjid and a.qsseq = input(b.idvarval,best.) and qnam='SOFASCS'
where qscat="SOFA" ;
quit;
proc sql;
create table qs01 as
select qs.*, a.*
from qs
full /* left? */ join adb.adsofa as a
on a.usubjid = qs.usubjid and a.qsseq = qs.qsseq and mittfl = "Y"
;
quit;
I assume that you did not really want to have a full join but a simple left loin in the last one.

Insert and update using proc sql simultaneously in SAS

I am importing a file using proc import
proc import datafile="/opt/Export_d.csv" replace
out=work.export_data; run;
THEN I SELECTED 2 VARIABLES FROM EXPORTED TABLE HAVING SOME CONDITION
proc SQL;
create table work.login2
as select ' SURVEY_ID'n, TIME format=datetime20.,' USERNAME'n
from work.export_data
having TIME=max(TIME) and ' USERNAME'="sasdemo";
quit;
then i created a macro for survey_id because i have to update a value with the Survey_id reference in different table shown in below code:
proc SQL;
select max(' SURVEY_ID'n) into: sid from work.login2;
quit;
now if the ID is blank then i don't have to insert anything but if we have survey_id we have update few columns in a table using below code which is not happening.
proc SQL;
case when ' SURVEY_ID'n is not null then update table_name set SAS_TRACKING_CODE="&Trackingcode."
where SURVEY_ID=&sid.
else end;
quit;
The syntax in your proc sql is not valid. At all. case whencan only be used within statemements like select, where, order by.
The following will do your update if SURVEY_ID is not blank:
proc sql;
update table_name
set SAS_TRACKING_CODE="&Trackingcode"
where SURVEY_ID=&sid. and SURVEY_ID is not null
;
quit;
If what you want is to execute the update only when a condition is true, you should use a macro.
EDIT: that macro would look like this:
%macro update;
%if %symexist(sid) and not &sid = %then %do;
proc sql;
update table_name
set SAS_TRACKING_CODE="&Trackingcode"
where SURVEY_ID=&sid.
;
quit;
%end;
%mend;

Comparing dates in sas

I am using proc sql on a data set and only want to include values where the date is before 2003. The dates in the dataset are in the format of DDMMMYYYY - for ex. 01JAN2004.
I'm trying to use:
proc sql;
select Name, Department, Join_Date format = WORDDATE.w.
from xxxxxxxxxxxxxxxx
where Join_Date < 01JAN2003;
quit;
But this does not work
Your date value isn't being recognised as such. Try:
proc sql;
select Name, Department, Join_Date format = WORDDATE.w.
from xxxxxxxxxxxxxxxx
where Join_Date < '01JAN2003'd;
quit;

SAS insert value with proc sql

So I have a rather interesting problem. I am trying to insert a current date in specific formats and styles, but for some reason it seems to fail. I know its not a formatting issue... But idk how to fix it. a data step solution is welcomed as well... Here's what works.
proc sql;
create table work.test
(test_Id char(50), test_Name char(50), cur_Mo char(1), cur_Qtr char(1), entered_Date char(8));
insert into work.test
values('201703','2017 Mar','0','0','24APR17')
values('201704','2017 Apr','0','0','24APR17')
values('201706','2017 Jun','1','0','23JUN17');
quit;
Here's what doesn't:
proc sql;
insert into work.test
values(catx('',put(year(today()),4.),case when month(today())< 10 then catx('','0',put(month(today()),2.)) else put(month(today()),2.)end) ,catx(' ',[put(year(today()),4.),put(today(),monname3.))],'1','0',put(today(),date7.));
quit;
You can use the %SYSFUNC() macro function to call most other SAS function in macro code. So to generate today's date in DATE7 format you could use:
insert into work.test (date)
values("%sysfunc(date(),date7)")
;
The way I'd probably do it is to use a data step to make a dataset that you would insert, and then insert that dataset.
You can use insert into (...) select (...) from (...) syntax in SAS, and the data step is much more flexible as to allowing you to define columns.
For example:
proc sql;
create table class like sashelp.class;
quit;
proc sql;
insert into class
select * from sashelp.class;
quit;
Or you can specify only certain variables:
proc sql;
insert into class (name, age)
select name, age from sashelp.class;
quit;
data to_insert;
name= 'Wilma';
sex = 'F';
age = 29;
height = 61.2;
weight = 95.3;
run;
proc sql;
insert into class
select * from to_insert;
quit;
Just make sure you either explicitly list the variables to insert/select, or you have the order exactly right (it matches up by position if you use * like I do above).

SAS MACRO FROM PROC SQL DATE ERROR

Hello I am trying to put an specific date from a dataset into a macro so i can use it in a DATA step , but i always get 01-JAN-1960 insteed of the date that i want
my code is the next one:
proc sql noprint ;
select WEEK_START
into :WEEK_START
from date_table
WHERE FW= 5;
quit;
%let start=&WEEK_START;
%LET TODAY= TODAY();
I made this so i can see the date that i want:
DATA TEMP;
DATE =&TODAY;
DATE1= &start;
FORMAT DATE DATE1 datE11.;
RUN;
And the result is:
DATE : 06-OCT-2014
DATE1 : 01-JAN-1960
proc sql noprint ;
select today()-1 as WEEK_START
into :WEEK_START
from maps.africa;
quit;
%let start=&WEEK_START;
%LET TODAY= TODAY();
DATA TEMP;
DATE =&TODAY;
DATE1= &start;
FORMAT DATE DATE1 datE11.;
RUN;
When I run this, both populate correctly.
Additionally, the value of Date1 in your example is the date value for the numeric value of 0. It looks like your original data is being populated incorrectly.