Hello,
I have variables with observations in different lengths as 000R201806, ZY201906
and I want to separate it into the letters (or digits) at the beginning of the string, the year, and the month:
000R 2018 04
ZY 2019 06
What code can I use?
Thank you,
Lior
@lioradam wrote:
Hello,
I have variables with observations in different lengths as 000R201806, ZY201906
and I want to separate it into the letters (or digits) at the beginning of the string, the year, and the month:
000R 2018 04
ZY 2019 06
What code can I use?
Thank you,
Lior
SUBSTR().
Note that it might be simpler to just create a DATE value instead of separate YEAR and MONTH values.
data have;
input string $20.;
cards;
000R201806
ZY201906
;
data want;
set have;
date = input(substrn(string,length(string)-5),yymmn6.);
format date yymmdd10.;
short_string=substr(string,1,length(string)-6);
year=year(date);
month=month(date);
run;
Result:
short_ Obs string date string year month 1 000R201806 2018-06-01 000R 2018 6 2 ZY201906 2019-06-01 ZY 2019 6
Always two digits for the month, four digits for the year, and the rest is the 'beginning of the string'?
Yes
Looks like @Tom beat me to it, but really you just need the length of the string, and then the last 6 characters are the year/month, and the rest are the "beginning of the string".
Thank you 👍
@lioradam wrote:
Hello,
I have variables with observations in different lengths as 000R201806, ZY201906
and I want to separate it into the letters (or digits) at the beginning of the string, the year, and the month:
000R 2018 04
ZY 2019 06
What code can I use?
Thank you,
Lior
SUBSTR().
Note that it might be simpler to just create a DATE value instead of separate YEAR and MONTH values.
data have;
input string $20.;
cards;
000R201806
ZY201906
;
data want;
set have;
date = input(substrn(string,length(string)-5),yymmn6.);
format date yymmdd10.;
short_string=substr(string,1,length(string)-6);
year=year(date);
month=month(date);
run;
Result:
short_ Obs string date string year month 1 000R201806 2018-06-01 000R 2018 6 2 ZY201906 2019-06-01 ZY 2019 6
Thank you very much!
Regards,
Lior
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.