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 |
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.
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;
@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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.