Can anyone help to find a way to extract all data between two dates (for example 01/10/2007 and 31/03/2008) in SAS please? Date format is DDMMYY10.
I tried the syntax below:
Data want; set have; where OrderDate between 01/10/2007 and 31/03/2008; Run;
it was giving me an error.
Thanks
data want;
set have;
where orderDate between '01OCT2007'd and '31MAR2008'd;
run;
Related
I am working with a huge dataset in sas trying to use proc sql and I need help setting up a like statement. I'm trying to extract all the columns that have 'eco' in the name
I'm getting an error in the where statement as it is not registering the second *.
Any help?
proc sql
select *
from cfy19e8
where * LIKE %eco%;
You could concatenate all of your columns with catx() and find any one that has the word eco.
data have;
input col1$ col2$ col3$;
datalines;
sadfeco kdoa wrfs
asdf asdf sadf
mfecosa mawoeco mfzeco
;
run;
data want;
set have;
where catx('|', col1, col2, col3) LIKE '%eco%';
run;
If you have a lot of character columns, you could use the shortcut _CHARACTER_ to concatenate all variables, then use find() within an if statement in a data step.
data want;
set have;
if(find(catx('|', of _CHARACTER_), 'eco') );
run;
Perhaps
proc contents noprint data=cfy19e8 out=eco_columns(where=(upcase(name) like '%ECO%'));
run;
title 'Columns with ECO in their name';
proc print data=eco_columns;
var name;
run;
I have two datatset Input12 and input18. Below is the code.
New12 dataset have variable score_date in format yymmn6.
data new12;
set input12;
run;
Now adding new variable score_date in dataset new18
%let score_date=201807;
data new18;
set input18;
format score_date yymmn6.;
run;
After concatenating the dataset new 12 and new18 the date format is not in yymmn6.
data new;
set new12 new18;
run;
This is giving informat date for new12 and blank for new18 in new
data new;
set new18 new12;
run;
This is giving correct date format for new18 and blank for new12 in new.
Is there any reason for improper format after concatenating.
Instead of relying on the source data sets for a variable's format, place a format statement in your data set stacking (concatenation) DATA step.
Example:
data new;
set
new12
new18
;
format score_date yymmn6.;
If you mean to have your date representation stored in a macro symbol, as I infer from your posted code, you need to input the representation using the appropriate informat in order to get a SAS date value.
score_date = input ("&score_date", yymmn6.);
format score_date yymmn6.;
An alternative is to set the macro symbol to the source code of a SAS date literal.
%let score_date = "01JUL2018"D;
and resolve that later in DATA Step as perhaps
data new18;
score_date = &score_date;
format score_date yymmn6.;
run;
I tried below codes.
%let score_date="01dec2019"d;
data twelvenew;
set twelve;
score_date=&scoredate.;
format score_date yymmn6.;
run;
%let score_date="01jun2019"d;
data eightnew;
set eight;
score_date=&scoredate.;
format score_date yymmn6.;
run;
data final;
set eightnew twelvenew;
run;
I am getting dates only eightnew in final dataset and others are missing.
Is iam missing anything here.
I have a SAS field where the datatype is number and format is date9.
It has a value like 30SEP2018.
How do I convert it to a SAS date so I can do date operations?
SAS dates are stored as number starting from 1/1/1960 and it starts form number = 0 and increases by 1 for every day. Your date is stored as number and then you get from proc contents and format is to display in the way you want.
data have;
input date:date9.;
format date date9.;
datalines;
30SEP2018
;
proc contents data=have;
run;
you can calculations on above date and gives you appropriate results as shown below
data want;
set have;
new_date= date+1;
new_date1= date-1;
format new_date new_date1 date9.;
run;
proc print; run;
I have a table in SAS where in one column, the date is stored (e.g. "2005/10").
How do I have to convert this to a SAS data format?
Among many other tries, I tried this code:
data test;
format date YYMMS.;
date = input(ObservationMonth, YYMMS.);
put date=date9.;
run;
You could just use the anydtdte. informat.
data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;
This informat detects most date formattings and converts the character-stored value to a SAS date.
One way is to use substr() and mdy() to extract the date components:
data _null_;
ObservationMonth ="2005/10";
date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
put date = date9.;
run;
Another option is to use the anydtdte informat (note that results may differ depending on your locale):
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth,anydtdte.);
put date = date9.;
run;
Yet another option is to modify the input to enable use of the YYMMDDw. informat:
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth!!'/01', YYMMDD10.);
put date = date9.;
run;
you're fantastic, guys! Thank you so much, with a "set" statement it works fine!
For some reason I'm not being able to read in the data properly. I want to be able to read in a large data set but within only specific dates such as Jan 2004 to FEB 2004. My Code is the following:
DATA Work.sales_fact;
SET Work.sales_fact_subset;
WHERE '01JAN2004'd <= Order_Date <= '14FEB2004'd;
RUN;
PROC PRINT;
RUN;
What am I doing incorrectly?
I think you have DATA and SET switched. DATA is what you want to create. SET is where the data is coming from.
DATA Work.sales_fact_subset ;
SET Work.sales_fact;
WHERE '01JAN2004'd <= Order_Date <= '14FEB2004'd;
RUN;
PROC PRINT data=Work.sales_fact_subset;
RUN;
Here is an example of this working..
Please check your dataset.
data want;
set sashelp.rent;
where "01feb1999"d <= date <= "02feb2003"d;
run;
If your table names and date structure is correct, your query is correct. Here is a sample of what I did with the correct result set.
data inputs;
input Date1 date9. ;
Format date1 date9.;
cards;
01JAN2004
02FEB2004
03MAR2004
04JUN2004
05JUL2004
;
DATA inputss;
SET inputs;
WHERE '01JAN2004'd <= Date1 <= '14FEB2004'd;
RUN;
PROC PRINT;
RUN;