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
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;
Best,
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
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;
Best,
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.