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

Hi,

 

I have a character date that need to be imputed with dates containing '00' (day) to '01' & '000' (month) to 'jan':

 

data:

00-000-2013

20-000-2013

00-dec-2013

 

want:

01-jan-2013

20-jan-2013

01-dec-2013

1 ACCEPTED SOLUTION

Accepted Solutions
qoit
Pyrite | Level 9

A simple SUBSTR should do it:

data have;
	infile datalines;
	input date_str $20.;
	format want_date_str $20.;
	want_date_str = date_str;

	if substr(date_str,1,2) = '00' then
		substr(want_date_str,1,2) = '01';

	if substr(date_str,4,3) = '000' then
		substr(want_date_str,4,3) = 'JAN';
	datalines;
00-000-2013
20-000-2013
00-dec-2013
;
run;

View solution in original post

2 REPLIES 2
qoit
Pyrite | Level 9

A simple SUBSTR should do it:

data have;
	infile datalines;
	input date_str $20.;
	format want_date_str $20.;
	want_date_str = date_str;

	if substr(date_str,1,2) = '00' then
		substr(want_date_str,1,2) = '01';

	if substr(date_str,4,3) = '000' then
		substr(want_date_str,4,3) = 'JAN';
	datalines;
00-000-2013
20-000-2013
00-dec-2013
;
run;
Phil_NZ
Barite | Level 11

Hi @qoit 

A great code, I also thought of using substr but I did not know that substr can be used to output.

Btw, just adjust the "JAN" in your code to "jan" to align with @HitmonTran 's requirement.

 

Warm.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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