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
Onyx | Level 15

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 521 views
  • 1 like
  • 3 in conversation