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,

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

 

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;

Thanks in Advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

I have adapted 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;

 

Capture d’écran 2020-05-13 à 13.46.13.png

Best,

View solution in original post

3 REPLIES 3
yabwon
Amethyst | Level 16

Read and learn about Perl Regular Expressions.

 

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

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

 

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

I have adapted 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;

 

Capture d’écran 2020-05-13 à 13.46.13.png

Best,

annypanny
Quartz | Level 8
thanks mam, it is working

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1104 views
  • 1 like
  • 3 in conversation