SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Himanshu4
Calcite | Level 5

Himanshu4_0-1675888003331.png

 

i have a variable in xlsx file having different format for date variable and need to upload it in SAS and create a dataset with date format. any suggestions??

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Are these stored in a character variable as text strings after you import into SAS? (Please don't tell me what they are in Excel, as that is irrelevant)

 

If they are text, you can possibly convert them into valid dates using the ANYDTDTE. informat which guesses what the format is and can (I think) handle different formatting on different rows.

--
Paige Miller
Himanshu4
Calcite | Level 5
After uploading it in excel its character, however data is populating like below, means. There is no specific format. And i would need that in a date format.
18Jan2020
45677

SASKiwi
PROC Star

Try changing the column type in Excel from General (the default) to Short Date then reimport.

Tom
Super User Tom
Super User

@Himanshu4 wrote:
After uploading it in excel its character, however data is populating like below, means. There is no specific format. And i would need that in a date format.
18Jan2020
45677


If you see both of those values in the same variable in the same dataset on different observations then your column in Excel has at least one cell that has a string in it instead of a date.  In this case it is the the string '18Jan2020'.  The other string '45677' is how SAS pulls the number that Excel uses to store the date 20JAN2025 (if it is using 1900 as the base date) or 20JAN2029 (if it is using 1904 as the base date).

2876  data _null_;
2877  excel=45677;
2878  sas=excel+'30dec1899'd ;
2879  put excel=comma7. sas=comma7. ' -> ' sas date9.;
2880  sas=excel+'31dec1903'd ;
2881  put excel=comma7. sas=comma7. ' -> ' sas date9.;
2882
2883  run;

excel=45,677 sas=23,761  -> 20JAN2025
excel=45,677 sas=25,222  -> 20JAN2029

To convert both those strings into actual date values you will need to do some testing to see which you have.

So you might assume that most of them were value Excel Date values and so first try to convert the 5 digit strings into dates and then when that did not work try to convert the string using the ANYDTDTE informat.

So if the dataset you created from the Excel sheet is named HAVE and the column that was supposed to have date values is named DATESTRING then here is data step that will make a new numeric variable named DATENUM that has actual date values and has a format attached that will print the date values in a way that humans can understand.

data want;
  set have;
  datenum = input(datestring,??32.)+'30dec1899'd;
  if missing(datenum) then datenum=input(datestring,anydtdte20.);
  format datenum date9.;
run;
Sajid01
Meteorite | Level 14

Hello @Himanshu4 
Please try importing first and let us know if you are facing error. 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1982 views
  • 0 likes
  • 6 in conversation