Date Pattern matching in SAS - sas

There is a variable in a dataset open_date (in the form datetime19. i.e. 08NOV2019:01:05:01 etc)
I need to perfom a patter check on the open_date
To verify as how may obs had date in format other that than datetime19.
Simply i need to apply patter check in open_date.. The valid format is datetime19.

Related

How do I change a date formatted like this "2017-03-24" into a proper date format in SAS?

I need to change what it is currently at into the date9 format.
input Company_Name$ 1-58 Form_Type$ 59-70 CIK Date_filed$ 86-96 File_Name$ 118-141;
length Date_filed $10.;
format Date_filed yymmdd10.;
ndate=put(Date_filed, yymmdd10.);
this is what I have, but it doesn't work. I am not sure how to do this.
SAS stores dates in numeric variables as the number of days since 1960. Once you have a valid date value you can attach any of the many formats that change how the value is displayed. To read directly from the source file into a date value using an INFORMAT that is appropriate for the value. Note that you cannot combine an informat with the column range in the input statement. Instead you can use # to position at the first column of the date string and then use formatted input. Also you did not tell INPUT what columns to read for the variable CIK.
input Company_Name $ 1-58 Form_Type $ 59-70
CIK 71-85 #86 Date_filed yymmdd10.
File_Name $ 118-141
;
format Date_filed date9.;
Note you were reading 11 columns before but date strings in YYYY-MM-DD style only need 10 columns, so check if your date strings are in columns 86-95 or columns 87-96. If you don't read the right columns you might miss the second digit of the day of the month or the first digit of the year.

How to parse format from given data?

I want to extend the question asked at Sum consecutive observations by some variable.
When I define the following macro variable
%let varnames=id dose supply date;
Since I don't know the fomat of the original data, how do I get the format for the rename automatically to apply it in the following extract of the above link?
data want(drop=id_i dose_i supply_i date_i);
format id dose supply 8. date mmddyy10.;
retain id dose supply date;
set have(rename=(id=id_i dose=dose_i supply=supply_i date=date_i)) end=last;

Convert SAS numeric dates to datetime format

I am trying to convert a numeric date into date-time format. I am first subtracting a few days from the date I have.
$let from_date = "21JUL2016:00:00:00"dt;
data _null_;
datediff = intnx('dtday',&from_dt,-180);
call symput("cutoff_dt",datediff);
run;
After this I get numeric date like 17633333 which is fine because I am using this numeric date in pass through queries. But I also need to convert this date into datetime format like "21Dec2015:00:00:00"dt so that I can use this date in proc sql as well. So far after searching through sas documents and blogs I have been unable to do this. Help please.
You can use PUT to apply the format, but if you need quotes and the dt then that's a different story. If you do need them, I think that the current macro variable would work as well. Otherwise you have some other issue going on. There is no requirement for SQL to require the formatted value, unless you need a character or you're passing it through to a DB. In those cases the dt won't be required either.
%let from_date = "21JUL2016:00:00:00"dt;
data _null_;
datediff = intnx('dtday',&from_dt,-180);
call symput("cutoff_dt", put(datediff, datetime21.));
run;
SAS stores dates times and datetime values as numbers. Just like in a datastep
where you can write "where x='19feb2016'd" or "where x=20503" which is the number that SAS stores for the date=19feb2016, you can do the same in proc sql.
You only need to write the date value out as a character value if the variable you are comparing to contains the date as a character string. that is y has the value "19feb2016" and x has the value 20503, the test if y=x will not achieve what you want. That is when you have to write if y=put(x,date9.);

Convert two numeric into a date format

I have two numeric variables, year and month. year variable has data such as 2010 and month variable has data such as 1 and 10 (1 through 9 doesn't have zero at the front). I need to combine these two variables and then convert it to YYMMn6. format so that I can merge another dataset based on the date.
For example, the input is:
2012 1
2012 10
The output I want is (in YYMMn6. format):
201201
201210
The codes I tried so far:
year1=close_year;
year2=clse_month;
yearmonth = cats(of year1-year2); *this results in character variable;
DATE2 = INPUT(PUT(yearmonth,8.),YYMMN6.);
FORMAT DATE2 YYMMN6.;
Of course I get an error message. Thanks.
With numeric variables I'd use MDY function rather than putting and whatnot; you're having trouble here because 20101 isn't a valid YYMM value.
dateval = mdy(monthval,1,yearval);
format dateval yymmn6.;
Note that the 'final' date format is wholly unrelated to whatever you use to input the date variable from an informat; there's no difference from SAS's point of view between
dateval = input('01JAN2010',DATE9.);
format dateval YYMMN6.;
and
dateval = input('201001',YYMMN6.);
format dateval YYMMN6.;
The input/informat is converting a value into a numeric number of days since 1/1/1960. The final format is telling SAS how to display that newly created number.
You can use the answer mentioned by Joe which
would give you the flexibility to change to a different format if you want later on,
without any hassle.
would keep the variables in numeric format, so mathematical or
date functions would be easy to apply.
or you can use
mydate=put(mdy(monthval,1,yearval),yymmn6.);
if you want the output in char format.
Both are correct. Choose as per your requiremnt.

How to add hyphen in values in SAS Enterprise Guide (to put in date format)?

I have a table in SAS that has a column of dates but are not in the typical date format (DD-MM-YY). For example, one of them is
21JUN2012:00:00:00
What can I do so that it is in the format
21-Jun-12
? I am using SAS Enterprise Guide so I know I would have to selected Computed Column but I do not know how to go from there. I was thinking of using TRANWRD but I can't seem to get it work.
I would appreciate any help, thanks.
Right-click on the column to be converted and select Properties.
Change the type to Numeric and click Yes in the warning prompt regarding precision.
Change the Group to Date.
Click Informats; the selected value under Categories should be Date.
Change the informat to DDMMYYSw.
Under Attributes, assign an overall width of 10
Click Formats; the selected value under Categories should be Date.
Change the format to DDMMYYSw.
Under Attributes, assign an overall width of 10.
Click OK
If the conversion results are correct in the Preview Results window,
click Commit changes.
Reference
There are many date formats and informats available in SAS to change the date format being presented. A full list of the formats can be found here in the SAS documentation. The format you want is ddmmyyd. and you can just apply the format to the date value (see col1_ example).
For datetime values, you need to use the DATEPART function to extract the date value before applying the format (see col2_ and col3_ examples below).
If your date is provided as text, you need to apply an informat to it to convert it to SAS date value and then you can apply your desired date format to the value using PUT statements. Including converting this value back to text (see col4_ examples below).
data date_infile;
infile datalines delimiter=',';
input col1_date :date9. col2_dtime :datetime18. col3_dtime :datetime18. col4_text $18.;
format col1_date date9. col2_dtime col3_dtime datetime18.;
datalines;
21jun2012,21JUN2012:00:00:00,21JUN2012:12:34:56,21JUN2012:00:00:00
11aug2012,11AUG2012:00:00:00,11AUG2012:01:23:45,11AUG2012:00:00:00
05oct2019,05OCT2019:00:00:00,05OCT2019:01:23:45,05OCT2019:00:00:00
;
run;
data date_results;
set date_infile;
format col1_date col2_date col3_date col4_date ddmmyyd.;
col2_date = datepart(col2_dtime);
col3_date = datepart(col3_dtime);
col4_date = datepart(input(col4_text, datetime18.));
col4_date_as_text = put(put(datepart(input(col4_text, datetime18.)), ddmmyyd.), $8.);
run;