BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8


I have a dataset, I want to check all the column name and if that consists "Date" in the header then convert that column in date9. format.

the already available date field can be in text format or date format, but they are date.

 

2 REPLIES 2
Kurt_Bremser
Super User

For an automatic conversion, you have to determine the following

- is the variable numeric or character?

- if the first:

  • are the contents typical for SAS dates -> only apply a format
  • are the contents typical for SAS datetimes -> use the datepart() function and apply a format
  • are the contents actually numbers like 20032018 or 20180320 -> convert with put() and input() functions and apply a format

if the latter:

  • find out which format is used
  • convert using the input() function with a proper informat
  • apply a format

Metadata (column name, type, currently applied format) can be retrieved from dictionary.columns (or sashelp.vcolumn)

 

This will be a very tedious task. It is much better to do this type of "cleaning" upon import of data into the SAS system.

Where does your dataset come from?

gamotte
Rhodochrosite | Level 12

Hello,

 

Assuming you have character dates as input :

 

data have;
    length date_1 date_2 $10;
    input id date_1 $ date_2 $;
    cards;
1 01/01/2018 05/16/2017
2 02/03/2018 06/25/2016
3 12/21/2017 03/01/2015
;
run;

data _NULL_;
    set have end=eof;

    call execute('data want; set have(rename=(');

    length colname $32;

    do while (upcase(colname) ne "COLNAME");
        call vnext(colname);
        if colname=:"date" then do;
            call execute(cats(colname,'=old_',colname));
        end;
    end;

    call execute('));');

    colname="";

    do while (upcase(colname) ne "COLNAME");
        call vnext(colname);
        if colname=:"date" then do;
            call execute(catx(' ','format',colname,'date9.;',colname,'=input(old_'||colname,', mmddyy10.);'));
        end;
    end;

    call execute('drop old_:; run;');

    stop;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 655 views
  • 0 likes
  • 3 in conversation