Home > Blockchain >  Getting Current date in SAS in YYMMDD10. Format in Date Datatype
Getting Current date in SAS in YYMMDD10. Format in Date Datatype

Time:05-05

I have a SQL Code which I am running in the SAS using proc SQL.

proc sql;
select col_A,col_B,put(date(),yymmdd10.) as col_C
from table_P;

create table_Q as 
(select * from table_P);
quit;

col_C is today's date and gets stored in the Varchar2 format. I want it to get stored in the date format. How do I do that? I want the date entries to look like '2022-05-04'. Even if the format of the date changes, how can I get into Date datatype from Varchar?

CodePudding user response:

create table table_Q as 
    select col_A
            , col_B
            , date() format=yymmdd10. as col_c 
    from table_P;

This will store col_C as a SAS date value, but apply the yyddmm10. format when presenting the data.

CodePudding user response:

If you have a character variable use the INPUT() function to convert it to a date value by using the YYMMDD10. informat.

Once you have a date value (number of days since 1960) you can then attach any date type format you want to have the date values print in the style you prefer.

So if COL_C in TABLE_P is the character variable then try:

In normal SAS code:

data table_Q;
  set table_P;
  datevar = input(col_c,yymmdd10.);
  format datevar date9.;
run;

In PROC SQL code:

proc sql;
  create table_Q as
    select a.*,input(col_c,yymmdd10.) as datevar format=date9.
    from table_P
  ;
quit;
  • Related