BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
lioradam
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Always two digits for the month, four digits for the year, and the rest is the 'beginning of the string'?

--
Paige Miller
lioradam
Obsidian | Level 7

Yes

PaigeMiller
Diamond | Level 26

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".

--
Paige Miller
lioradam
Obsidian | Level 7

Thank you 👍

Tom
Super User Tom
Super User

@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
lioradam
Obsidian | Level 7

Thank you very much!

 

Regards,

Lior

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 711 views
  • 3 likes
  • 3 in conversation