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

Hi Community,

suppose there is a collection table as below,

Sc_no  Strategy  Collection_name

A0007   FLEXI    Flexi 2035-37 (BLK)
A0008   FLEXI    itl      2037-39 (BLK)
A0009   FLEXI    cptl   2040-42 (BLK)
A0010   FLEXI    sen   2038-40 (BLK)

I have to calculate the year which is 20 years from run date. E.g if today’s date is 12MAY2020, then this step will give 2040. And it will pick only that collection whose time range meets 2040. Example from the above table, the last two rows i.e., cptl  2040-42 (BLK), sen  2038-40 (BLK) the time ranges meets 2040. then this will be picked up automatically.

How can I do that

Thanks in advance   

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

Here is a way to do this.

The prxchange function identifies in the year boundaries in the pattern of each collection name.

Best,

data have;
	infile datalines dlm="," dsd;
	input Sc_no $ Strategy $ Collection_name :$20.;
	datalines;
A0007,FLEXI,"Flexi 2035-37 (BLK)"
A0008,FLEXI,"itl      2037-39 (BLK)"
A0009,FLEXI,"cptl   2040-42 (BLK)"
A0010,FLEXI,"sen   2038-40 (BLK)"
;
run;

data want;
	set have;
	_low_bound  = prxchange('s/^.*(\d{2}\d{2})-\d{2}.*$/$1/i',1,Collection_name);
	_high_bound = prxchange('s/^.*(\d{2})\d{2}-(\d{2}).*$/$1$2/i',1,Collection_name);;
	if _low_bound <= year(today())+20 <= _high_bound then output;
	drop _:;
run;

image.png

View solution in original post

6 REPLIES 6
ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

Here is a way to do this.

The prxchange function identifies in the year boundaries in the pattern of each collection name.

Best,

data have;
	infile datalines dlm="," dsd;
	input Sc_no $ Strategy $ Collection_name :$20.;
	datalines;
A0007,FLEXI,"Flexi 2035-37 (BLK)"
A0008,FLEXI,"itl      2037-39 (BLK)"
A0009,FLEXI,"cptl   2040-42 (BLK)"
A0010,FLEXI,"sen   2038-40 (BLK)"
;
run;

data want;
	set have;
	_low_bound  = prxchange('s/^.*(\d{2}\d{2})-\d{2}.*$/$1/i',1,Collection_name);
	_high_bound = prxchange('s/^.*(\d{2})\d{2}-(\d{2}).*$/$1$2/i',1,Collection_name);;
	if _low_bound <= year(today())+20 <= _high_bound then output;
	drop _:;
run;

image.png

annypanny
Quartz | Level 8

Hi mam

it is running fine for this type of range 2040-42 but when I used 2040-2042, then the code is not returning anything. Can You help me in this

 

ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

I have adapte the code to take into account this situation:

data have;
	infile datalines dlm="," dsd;
	input Sc_no $ Strategy $ Collection_name :$40.;
	datalines;
A0007,FLEXI,"Flexi 2035-37 (BLK)"
A0008,FLEXI,"itl      2037-39 (BLK)"
A0009,FLEXI,"cptl   2040-42 (BLK)"
A0010,FLEXI,"sen   2038-40 (BLK)"
A0010,FLEXI,"sen   2038-2042 (BLK)"
;
run;

data want;
	set have;
	_low_bound  = prxchange('s/^.*(\d{2}\d{2})-\d{2}.*$/$1/i',1,Collection_name);
	
	/*Second year in format yy e.g. 2038-42 */
	if prxmatch('/^.*(\d{2})\d{2}-(\d{2})\D*$/i',Collection_name) then
		_high_bound = prxchange('s/^.*(\d{2})\d{2}-(\d{2}).*$/$1$2/i',1,Collection_name);

	/*Second year in format yyyy e.g. 2038-2042 */
	else if prxmatch('/^.*(\d{2})\d{2}-(\d{4})\D*$/i',Collection_name) then
		_high_bound = prxchange('s/^.*(\d{2})\d{2}-(\d{4}).*$/$2/i',1,Collection_name);
	
	if _low_bound <= year(today())+20 <= _high_bound then output;
	
	drop _:;
run;

proc print;

Best,

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