BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

Hi All,

My date field (Imported from Excel) written like 2/30/2015 and have $10. format. Can someone help me to convert it to 20150230 format please. Thanks,

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @mlogan,

 

I would create SAS date values from those character dates:

data _null_;
length c $10;
c='3/30/2015';
d=input(c,mmddyy10.);
format d yymmddn8.;
put d;
run;

If you need a character string in YYYYMMDD format you can apply the PUT function to the SAS date value:

dc=put(input(c,mmddyy10.),yymmddn8.);

 

For invalid dates the conversion will fail, though. 🙂

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @mlogan,

 

I would create SAS date values from those character dates:

data _null_;
length c $10;
c='3/30/2015';
d=input(c,mmddyy10.);
format d yymmddn8.;
put d;
run;

If you need a character string in YYYYMMDD format you can apply the PUT function to the SAS date value:

dc=put(input(c,mmddyy10.),yymmddn8.);

 

For invalid dates the conversion will fail, though. 🙂

mlogan
Lapis Lazuli | Level 10
Thanks Reinhard.
mlogan
Lapis Lazuli | Level 10
Hi Reinhard,
Would you please tell me what I have to do if I want to convert the existing column to YYMMDDN8. format without making a new column. Here in your example converting column 'c' without making a new column 'd'.
FreelanceReinh
Jade | Level 19

You could drop the old (character) variable and rename the new (numeric) one:

data have;
length c $10;
c='3/30/2015';
run;

data want;
set have;
d=input(c,mmddyy10.);
format d yymmddn8.;
drop c;
rename d=c;
run;

If your goal is a character variable (such as DC in my previous post), you could overwrite the existing value directly, because no type conversion would occur:

data want;
set have;
c=put(input(c,mmddyy10.),yymmddn8.);
run;

However, this would leave the length of the variable at 10, although 8 would be sufficient. To avoid this, you could define a new variable with length 8 and then drop and rename as shown above.

 

mlogan
Lapis Lazuli | Level 10
Thanks Reinhard, I did not know this drop trick. Very helpful.
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
  • 5 replies
  • 12632 views
  • 2 likes
  • 2 in conversation