BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
stevenyan0127
Fluorite | Level 6

Hi everyone, 

I would like to:

• convert all date formats into mmddyy10.

• All of these variables are in numeric form

• For those cells without year value, I would like to add year 2020

Here's a preview of the data: 

ABCDEFGHIJKL
3/20/204/2/203/21/20204/1/20208-Apr12-Aug10-Jun10-Jun12-Aug4-Sep8/12/202012-Aug
4/7/205/1/204/10/20204/24/20209-Jun8-Jun19-Oct21-Oct  1/15/202115-Jan
4/6/205/11/204/6/20205/3/202025-Jun  1-Oct    
3/16/204/16/203/17/20204/8/20206-May11-Aug 17-Jun11-Aug   
3/27/204/12/203/30/20204/10/20201-May19-Jun19-May19-May18-May   
3/20/204/13/203/25/20204/10/20202-May13-Nov2-Jun2-Jun4-Aug3-Sep8/4/20204-Aug
3/27/204/16/203/27/20204/10/20201-May28-Jul25-Jul29-May27-Jul 7/28/2020 
3/26/204/11/203/30/20204/9/20207-May2-Jun21-Jul21-Jul21-Jul10-Sep  
4/14/205/10/204/16/20205/6/202012-Jun 26-Aug     
3/18/204/18/203/22/20204/5/202016-May20-Oct28-Jul10/27/202020-Oct22-Sep10/27/202010/27/2020
4/18/207/5/204/19/20206/15/202010-Aug 8/21/202123-Dec    
4/24/206/28/204/29/20206/15/20207/9/20204-Sep3-Aug 18-Jul 3/11/2021 
3/25/204/12/203/29/20204/8/20205-May4-Aug4-Aug4-Aug4-Aug 11/5/2020 

I have attached the data below. I would appreciate all the help. Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you already have a SAS dataset you can use the FMTINFO() function to find which of the variables are dates so help automate changing the format attached to them.

 

proc import datafile='c:\downloads\dates.xlsx' dbms=xlsx out=dates replace;
run;

proc contents noprint data=dates out=contents;
run;

proc sql noprint;
  select nliteral(name) into :dates separated by ' ' 
  from contents
  where 'date'=fmtinfo(format,'cat')
  ;
%let ndates=&sqlobs;
quit;

%if (&ndates) %then %do;
proc datasets nolist lib=work;
  modify dates;
    format &dates date9.;
  run;
quit;
%end;

proc print data=dates(obs=5);
run;

Results

Obs         A         B         C         D         E         F         G         H         I         J         K         L

  1 20MAR2020 02APR2020 21MAR2020 01APR2020 08APR2022 12AUG2022 10JUN2022 10JUN2022 12AUG2022 04SEP2022 12AUG2020 12AUG2022
  2 07APR2020 01MAY2020 10APR2020 24APR2020 09JUN2022 08JUN2022 19OCT2022 21OCT2022         .         . 15JAN2021 15JAN2022
  3 06APR2020 11MAY2020 06APR2020 03MAY2020 25JUN2022         .         . 01OCT2022         .         .         .         .
  4 16MAR2020 16APR2020 17MAR2020 08APR2020 06MAY2022 11AUG2022         . 17JUN2022 11AUG2022         .         .         .
  5 27MAR2020 12APR2020 30MAR2020 10APR2020 01MAY2022 19JUN2022 19MAY2022 19MAY2022 18MAY2022         .         .         .

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Here is how this FORUM displays the file you attached.

Screenshot 2022-03-25 114442.jpg

That sheet is going to cause trouble for importing into SAS as it appears to have at least one extra row above the actual place where the tabular data layout starts.  But assuming you use the right code to import just the data table then only C and D look like they contain text.  The other fields look like they contain dates and should be imported properly as SAS date values.  The ones that do not appear to have a year are right aligned so they are probably numeric values so the year is there, it is just not being displayed on the screen. Once you have SAS date values you can attach any format that knows how to display date values, so let's use DATE9. in our example code.

format A B E--L date9.;

To convert C and D into dates you will need to use the INPUT() function to make new numeric variables.  They appear to be using M-D-Y order in those strings so use the MMDDYY informat.

c_date = input(c,mmddyy10.);
d_date = input(d,mmddyy10.);
format c_date d_date date9.;

 

stevenyan0127
Fluorite | Level 6
Hi, thanks Tom! I have re-uploaded the sheet and should appear normal now. I will try your codes.
Tom
Super User Tom
Super User

So that version does not have any issues.  PROC IMPORT makes everything a date.

The CONTENTS Procedure

    Alphabetic List of Variables and Attributes

 #    Variable    Type    Len    Format      Label

 1    A           Num       8    MMDDYY8.    A
 2    B           Num       8    MMDDYY8.    B
 3    C           Num       8    MMDDYY8.    C
 4    D           Num       8    MMDDYY8.    D
 5    E           Num       8    DATE5.      E
 6    F           Num       8    DATE5.      F
 7    G           Num       8    DATE5.      G
 8    H           Num       8    DATE5.      H
 9    I           Num       8    DATE5.      I
10    J           Num       8    DATE5.      J
11    K           Num       8    MMDDYY8.    K
12    L           Num       8    DATE5.      L

Use whatever date type format you want with any of those variables.  I would avoid either MMDDYY or DDMMYY as either choice will confuse half of your audience.  Use DATE9. or YYMMDD10. .

data_null__
Jade | Level 19

Looks like they are all EXCEL dates.

A	B	C	D	E	F	G	H	I	J	K	L
43910	43923	43911	43922	43929	44055	43992	43992	44055	44078	44055	44055
43928	43952	43931	43945	43991	43990	44123	44125			44211	44211
43927	43962	43927	43954	44007			44105				
43906	43937	43907	43929	43957	44054		43999	44054			
43917	43933	43920	43931	43952	44001	43970	43970	43969			
43910	43934	43915	43931	43953	44148	43984	43984	44047	44077	44047	44047
43917	43937	43917	43931	43952	44040	44037	43980	44039		44040	
43916	43932	43920	43930	43958	43984	44033	44033	44033	44084		
43935	43961	43937	43957	43994		44434					
43908	43939	43912	43926	43967	44124	44040	44131	44124	44096	44131	44131
43939	44017	43940	43997	44053		44429	44553				
43945	44010	43950	43997	44021	44078	44046		44030		44266	
43915	43933	43919	43929	43956	44047	44047	44047	44047		44140	

 

 

A B C D E F G H I J K L
43910 43923 43911 43922 43929 44055 43992 43992 44055 44078 44055 44055
43928 43952 43931 43945 43991 43990 44123 44125     44211 44211
43927 43962 43927 43954 44007     44105        
43906 43937 43907 43929 43957 44054   43999 44054      
43917 43933 43920 43931 43952 44001 43970 43970 43969      
43910 43934 43915 43931 43953 44148 43984 43984 44047 44077 44047 44047
43917 43937 43917 43931 43952 44040 44037 43980 44039   44040  
43916 43932 43920 43930 43958 43984 44033 44033 44033 44084    
43935 43961 43937 43957 43994   44434          
43908 43939 43912 43926 43967 44124 44040 44131 44124 44096 44131 44131
43939 44017 43940 43997 44053   44429 44553        
43945 44010 43950 43997 44021 44078 44046   44030   44266  
43915 43933 43919 43929 43956 44047 44047 44047 44047   44140  
Tom
Super User Tom
Super User

If you already have a SAS dataset you can use the FMTINFO() function to find which of the variables are dates so help automate changing the format attached to them.

 

proc import datafile='c:\downloads\dates.xlsx' dbms=xlsx out=dates replace;
run;

proc contents noprint data=dates out=contents;
run;

proc sql noprint;
  select nliteral(name) into :dates separated by ' ' 
  from contents
  where 'date'=fmtinfo(format,'cat')
  ;
%let ndates=&sqlobs;
quit;

%if (&ndates) %then %do;
proc datasets nolist lib=work;
  modify dates;
    format &dates date9.;
  run;
quit;
%end;

proc print data=dates(obs=5);
run;

Results

Obs         A         B         C         D         E         F         G         H         I         J         K         L

  1 20MAR2020 02APR2020 21MAR2020 01APR2020 08APR2022 12AUG2022 10JUN2022 10JUN2022 12AUG2022 04SEP2022 12AUG2020 12AUG2022
  2 07APR2020 01MAY2020 10APR2020 24APR2020 09JUN2022 08JUN2022 19OCT2022 21OCT2022         .         . 15JAN2021 15JAN2022
  3 06APR2020 11MAY2020 06APR2020 03MAY2020 25JUN2022         .         . 01OCT2022         .         .         .         .
  4 16MAR2020 16APR2020 17MAR2020 08APR2020 06MAY2022 11AUG2022         . 17JUN2022 11AUG2022         .         .         .
  5 27MAR2020 12APR2020 30MAR2020 10APR2020 01MAY2022 19JUN2022 19MAY2022 19MAY2022 18MAY2022         .         .         .

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1843 views
  • 2 likes
  • 3 in conversation