Comparing dates in sas - 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;

Related

Proc sql how to pull member records greater than 18yrs using birthday

column how the birthdate is listed
birth_date
1933-11-25
1924-04-04
1951-11-15
1933-11-25
1956-11-20
1956-11-20
1942-02-25
1945-05-03
1954-09-16
1956-04-09
1952-02-15
1952-02-15
proc sql;
select * from table
where birth_date>18years;
quit;
You can use INTNX() function to find the cutoff date.
where birth_date <= intnx('year',today(),-18,'same')

Select a date and format output

Below is my code of 5 lines. When I run the first 3 lines I get a date output of 21042 and would like it displayed/formatted as 8/11/2017. I am having trouble with the format part (line 4) and need help with it. My code is:
PROC SQL;
select max (Load_DT) as max_date
from in.db_tb
Format max_date yymmdd10.;
quit;
You need to put the format statement in the select part of the query.
data db_tb;
load_dt = today();
run;
PROC SQL;
select max (Load_DT) as max_date format yymmdd10.
from db_tb ;
quit;
Note your stated preference (8/11/2017) does not match the format you use in your code (2017-08-11). MMDDYY10. is the format that you'd want for that.

Exporting data from SAS to Excel with a custom file name

I need to export a data set from SAS to Excel 2013 as a .csv file. However, I need the file name to be dynamic. In this instance, I need it to appear as:
in_C000000_013117_65201.csv
where the string, "in_C000000_" will remain constant, the string "013117_" will be the current day's date, and the string "65201" will be the row count of the data set itself.
Any help that you can provide would be much appreciated!
Thanks!
Here's a modified macro I wrote in the past that does almost exactly what you're asking for. If you want to replace sysdate with a date in your desired format, that's easy to do as well:
%let path = [[desired destination]];
%macro exporter(dataset);
proc sql noprint;
select count(*) into: obs
from &dataset.;
quit;
data temp;
format date mmddyy6.;
date = today();
run;
proc sql noprint;
select date format mmddyy6. into: date_formatted
from temp;
quit;
proc export data = &dataset.
file = "&path.in_C000000_&date_formatted._%sysfunc(compress(&obs.)).csv"
dbms = csv replace;
run;
%mend exporter;
%exporter(your_dataset_here);
Produces datasets in the format: in_C000000_020117_50000.csv

How to use DATETIME in WHERE clause in SAS PROC SQL

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;

SAS SQL Date formatting

Just wondering how I can alter the following query to show date in the format I want. I am using SAS to pull this data.
Existing Date format: 15MAR2011:09:05:16.000000
Format I want: 15MAR2011:09:05:16
Query I am using:
proc sql;
create table data.test as
select * from connection to odbc
(
select ID,
DATE AS CREATION_DATE,
from maintable
);
quit;
A format affects how SAS displays a variable value. It does not affect the actual value itself.
So, assuming the variable CREATION_DATE is a datetime value, just assign it a format of DATETIME20. to display is as you want:
proc sql;
create table data.test as
select ID, CREATION_DATE format=datetime20.
from connection to odbc
( select ID, DATE AS CREATION_DATE
from maintable );
quit;
However, some ODBC interfaces will return your date column as a character string, so you need to be sure it is showing up on the SAS side as a proper datetime value.