BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I've a variable (Test) values as follows. It's a character variable. I want to convert only the value 21183 to 30DEC2017 and still it should be a character variable.

 

Data As is:

 

Test
21183-23-TUSE-GHU

 

Data To be:

 

Test
30DEC2017-23-TUSE-GHU

 

3 REPLIES 3
Kurt_Bremser
Super User
data test;
length test $100;
test = "21183-23-TUSE-GHU";
index = indexc(test,'-');
test = put(input(substr(test,1,index-1),5.),date9.) !! substr(test,index);
drop index;
run;

Ask yourself if it wouldn't be better to use YYMMDDD10. instead of DATE9.

PeterClemmensen
Tourmaline | Level 20

One way

 

data _null_;
    a='21183-23-TUSE-GHU';
    b=cats(put(input(scan(a, 1, '-'), 8.), date9.), substr(a, indexc(a,'-')));
    put b=;
ruN;
ballardw
Super User

@David_Billa wrote:

I've a variable (Test) values as follows. It's a character variable. I want to convert only the value 21183 to 30DEC2017 and still it should be a character variable.

 

Data As is:

 

Test
21183-23-TUSE-GHU

 

Data To be:

 

Test
30DEC2017-23-TUSE-GHU

 


Where did the 21183 come from in the first place? If you created that value then show use how as that would be the place to do create the correct value to begin with.

 

BTW replacing 5 characters with 9 in the same variable may not be desired as you may exceed the current defined length of the variable and have truncation as a result.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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