Reading SAS date values - sas

My data set has one variable whose values is like MMMYYYY format. But it is in character.
I have used input function to convert it to numeric but it didnt work.
code i have used is
newdate=input(chardate, date9.);
but is is not working. Please suggest me whats wrong in this code.
Thanks,
Ravi

You date isn't in a Date9 format, it's in the MONYY INFORMAT
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000199690.htm
newdate=input(chardate, monyy7.);

Related

Wrong date output

I am trying to convert simple date to date9. format.
%let snapshot_date=201806;
%let dt0=%sysfunc(intnx(month,%sysfunc(inputn(&snapshot_date.,yymmn6.)),0,b),yymmn6.);
data new;
set sample;
format cutoff_date date9.;
cutoff_date=input(&dt0.,anydtdte11.);
run;
I am getting cutof_date as 28jun2020 instead of 30jun2018. Is iam doing anything wrong here.
So the macro statements start with a YYYMM string. Convert it to the first day of the month using INPUTN() function. Then convert it from that date back to exact same date using INTNX() function with an interval of zero. (Perhaps in your real problem the interval is not zero?). Then convert it back to a new YYYYMM string.
The SAS code you are generating is :
cutoff_date=input(201806,anydtdte11.);
That is trying to convert the number 201,806 into a date using the ANYDTDTE11. informat. Since the INPUT() function needs a string and not a number as its input SAS will convert the number 201,806 into a string using the BEST12. format. So it runs this code:
cutoff_date=input(" 201806",anydtdte11.);
The ANYDTDTE informat has to decides to map those 6 characters into month, day and year so it splits into three parts 20 18 06. Since the first two are larger than 12 one must be day and the other year. It decides it is Y/D/M order. Not sure why as I have never seen that order used in real life.
Instead use the same informat in the SAS code that was used in the macro code. So to convert the string 201806 in SAS code you would use either of these statements:
cutoff_date=input("201806",yymmn6.);
cutoff_date=inputn("201806","yymmn6.");
To generate that from your macro variable you need to add the quotes. So use:
cutoff_date=input("&dt0.",yymmn6.);
SAS interprets dates as the number of days since Jan 1st 1960, and you are supplying a number to the input function which is designed to convert characters to numbers. anydtdte. is interpreting it incorrectly as a result. Put quotes around &dt0 and use the yymmn6. informat instead so that SAS converts it to a date correctly.
data new;
format cutoff_date date9.;
cutoff_date=input("&dt0.",yymmn6.);
run;
Output:
cutoff_date
01JUN2018
anydtdte. will not work here since yymmn6. is not in the list of formats it tries to read. A list of the date types it will read is located here:
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n04jh1fkv5c8zan14fhqcby7jsu4.htm?homeOnFail

Character conversion from SAS to TERADATA

I have recently started a new job and I am using tools which I am not very familiar with. so i was wondering if the StackOverFlow family could help me out.
I have this concatenation in SAS, but I am not able to sort it out on TERADATA
t1.COD_CZ||PUT(INPUT(t1.CODTC,5.),z4.)||PUT(t1.PROGOPE,z8.) as CODIGO_MCT
I have written something like this, but then the length of the string is not harmonized with the the result in sas.
t1.COD_CZ|| cast(cast(t1.CODTC as int) as char(4))|| cast(t1.PROGOPE as char(8)) as CODIGO_MCT
Can you gently enlight me? thanks in advance
You must apply a FORMAT to get leading zeroes (concatenating trims trailing spaces):
t1.COD_CZ||Cast(t1.CODTC AS FORMAT '9(4)')||Cast(t1.PROGOPE AS FORMAT '9(8)')
The result has a fixed length, but it's still a VarChar(17). If you need fixed length, e.g. for export:
CAST(t1.COD_CZ||Cast(t1.CODTC AS FORMAT '9(4)')||Cast(t1.PROGOPE AS FORMAT '9(8)') AS CHAR(17))

Convert date9. to character

I'm trying to convert a sas date9. date to a character variable,
but the problem, I guess, is that date9. actually has a numeric "julian" value, so when I try to pass it to a character variable, it dismisses the date9. format and becomes a number ("21635").
In other words, I have a variable date9. = 27MAR2019, with the value "21635", and I want a character variable char = "27MAR2019".
I tried using both put and input functions, but they only use the 'julian' value.
Can anyone give me a hand?
The number you are showing is the number of days since 1960. That is how SAS stores dates. If you want the FORMATTED value of a variable instead of the raw value of the variable you need to ask for it. For example by using the PUT() function
newvar=put(oldvar,date9.);
or the VVALUE() function.
newvar=vvalue(oldvar);

Create a macro with format YYYY-MM-DD HH:MM_SS in SAS Enterprise Guide

I need to put this date in sas with a macro: '2019-01-14 00:00:00'
Now I have:
%let fecha=%SYSFUNC(PUTN(%SYSFUNC(TODAY()),E8601DT20.));
And the result is:
1960-01-01T05:59:31
But it's not right, today isn`t 1960 and I need the date without the T
Can anybody help me?
You cannot apply a datetime format to a date value. Dates are stored as number of days and Datetime is stored as number of seconds.
%let fecha=%SYSFUNC(TODAY(),yymmdd10) 00:00:00;
If you want to use it as a string then just add quotes. For example you could use this statement to create a character variable in a data step from the macro variable's value.
datestr = "&fecha";
But if you require the single quotes (perhaps to generate pass thru SQL code) then it is a little harder because the macro processor normally ignores strings inside single quotes.
%let fecha2=%unquote(%bquote('%SYSFUNC(TODAY(),yymmdd10) 00:00:00'));

Permanently Reformat Variable Values in SAS

I am trying to reformat my variables in SAS using the put statement and a user defined format. However, I can't seem to get it to work. I want to make the value "S0001-001" convert to "S0001-002". However, when I use this code:
put("S0001-001",$format.)
it returns "S0001-001". I double-checked my format and it is mapped correctly. I import it from Excel, convert it to a SAS table, and convert the SAS table to a SAS format.
Am I misunderstanding what the put statement is supposed to be doing?
Thanks for the help.
Assuming that you tried something like this it should work as you intended.
proc format ;
value $format 'S0001-001' = 'S0001-002' ;
run;
data want ;
old= 'S0001-001';
new=put(old,$format.);
put (old new) (=:$quote.);
run;
Make sure that you do not have leading spaces or other invisible characters in either the variable value or the START value of your format. Similarly make sure that your hyphens are actual hyphens and not em-dash characters.