Date format in SAS producing xxxxxx data - sas

When I am assigning a date variable to another new variable it is coming as all XXXX
so new_date = old_date where old_date was showing as 2021-09-09:00:00:00 but the new date in the new dataset is showing with all xxxxxx. I tried formatting it to date9. but no luck.
sales_date = purchase_date;
Any advice?
I tried format date9.

Dates are stored differently than datetimes by SAS.
That means you cannot apply date formats to a datetimes and vice versa.
Fortunately, SAS provides a couple of functions to deal with this situation. To extract the date value from a datetimes, you should use datepart:
data test;
attrib a_dt format=datetime.;
attrib a_d format=date9.;
*-- Init datetime --*;
a_dt = "08DEC2022:07:48:00"dt;
*-- Extract date from datetime--*;
a_d = datepart(a_dt);
output;
run;

Related

How to convert dd-mmm-yyyy to date9 in sas

Want to convert date = 01-SEP-2021 to date = 01SEP2021. I used this code to convert:
New date = input(compress(date,'-'),date9.);
But that didn't work. I also used substring to remove the parts of day month and year but seems this method is a bit lengthy.
If the type is numeric and SAS already displays it as a date then it's a formatting issue.
proc datasets lib=work nolist;
modify have;
format date date9.;
quit;
Otherwise, it must be a character and you have to convert it to a date and display it in date9. format.
data want;
date = '01-SEP-2021';
new_date = input(date, anydtdte.); *anydtdte. automatically reads a variety of date formats;
format new_date date9.;
run;

Extremely New to SAS

I am new to SAS and I am struggling struggling with my code. I would love some help. Am I thinking about this the right way? I have a huge table and I want to extract that data from certain dates. My two dates: 1969-12-01 and 1948-01-01 my sample code:
data null;
call symput ('timenow',put (time(),time.));
call symput ('datenow',put (date(),date9.));
run;
title "The current time is timenow and the date is datenow";
proc print data=sashelp.buy;
run;
So first learn about your dataset. So for example run PROC CONTENTS.
proc contents data=sashelp.buy; run;
Which will show you that there is variable named DATE that has date values (number of days since 1960).
So to reference a specific date use a date literal. That is a quoted string in the style that the DATE informat can read followed by the letter D. You can then use a WHERE statement to filter the data.
data want;
set sashelp.buy;
where date = '31dec1969'd ;
run;
Which will not find any observations since that date does not appear in that dataset.
If you want to select for multiple dates you could either add more conditions using OR.
where (date = '31dec1969'd) or (date = '01jan1948'd);
You can also use the IN operator:
where date in ('31dec1969'd '01jan1948'd);
Note that if your variable contains datetime values (number of seconds) then to pick a specific date you would either need to use a range of datetime literals:
where datetime between '31dec1969:00:00'dt and '31dec1969:11:59:59'dt);
Or convert the number of seconds into number of days and compare to the date literal.
where datepart(datetime) = '31dec1969'd ;
Welcome to StackOverflow Sportsguy3090.
Here I make a dataset called sample with some sample dates. That dataset has a variable called name and another variable called date. Internally, SAS stores dates as the number of days until or after January 1st 1970. That is rough to look at. So I use the format statement to have the dates appear as a 10 character string with month/day/year.
data sample;
name = "Abe "; date = "01Dec1969"d; output;
name = "Betty"; date = "01Jan1948"d; output;
name = "Carl"; date = "06Jun1960"d; output;
name = "Doug"; date = "06Dec1969"d; output;
name = "Ed"; date = "01Jan1947"d; output;
format date mmddyy10.;
run;
The code below subsets the data and puts the good records into a new dataset called keepers. It only keeps the records that are in the date range (including the limit dates).
data keepers;
set sample;
where date between "01jan1948"d and "01Dec1969"d;
run;
I hope that helps.... if not send up another flare.

Sas date format concatenating

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 would like to get Date and Time separately from DATETIME. format

I would like to get only date and time separately from 01JAN13:08:29:00
Format & Infomat available in Dataset is:
Date Num 8 DATETIME.(format) ANYDTDTM40(informat)
And If I run datepart() on 01JAN13:08:29:00 I get output as 19359 (I don't want it.)
The DATEPART function extracts the date value from a datetime value. The date value as you have seen is simply a number. A date format must be applied to a variable holding a date value. Base SAS variables have only two value types, character and numeric.
data want;
now_dtm = datetime();
now_dt = datepart(now_dtm);
now_dt_unformatted = now_dt;
format now_dtm datetime.;
format now_dt date9.; * <----- this is what you need, format stored in data set header information;
run;
proc print data=want;
run;
* you can change the format temporarily during a proc step;
proc print data=want;
format now_dt yymmdd10.; * <---- changes format for duration of proc step;
format now_dt_unformatted mmddyy10.;
run;
Actually 19,359 is exactly the value you want. You started with the number of seconds since 1960 and converted it to the number of days since 1960.
data x ;
dt = '01JAN13:08:29:00'dt ;
date = datepart(dt);
time = timepart(dt);
put (dt date time) (=);
run;
Results
dt=1672648140 date=19359 time=30540
You just need to attach a format to your new variable so that SAS will display the value in a format that humans will recognize. You could use a format like DATE9. to have it show 19,359 as 01JAN2013. Similarly you need to attach a format to the time part to make it print in format that human's will interpret as a time.
format date date9. time time8. ;

How to convert string to date in SAS?

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!