Hi, I am not getting why this program is not giving me expected result. Is there any issue of compiling prxparse as I was expecting atleast one of b c d variable have value. but it is only populated for D for first row as it was having cat. but i was expecting c and d also populated for second and third row bit it is not. Is there any specific regarding prxparse that we cannot use for multiple time in same code.
data test1;
a = 'there is a cat' ;
output;
a = 'IT IS UNIT1A' ;
output;
a = 'IT IS unit' ;
output;
run;
data test2;
set test1;
retain patternID_1 patternID_2 patternID_3;
if _n_ = 1 then do;
patternID_1 = prxparse('/UNIT1A$/i');
patternID_2 = prxparse('/UNIT$/i');
patternID_3 = prxparse('/cat$/i');
end;
b = prxmatch(patternID_1,a);
c = prxmatch(patternID_2,a);
d = prxmatch(patternID_3,a);
run;
proc print data=test2;run;
Hi @nkm1
It seems that SAS adds trailing blanks at the end of the 'a' variable:
Test:
data test2;
set test1;
i=tranwrd(a," ","X");
run;
To correct it, you can either do :
data test2;
set test1;
retain patternID_1 patternID_2 patternID_3;
if _n_=1 then do;
patternID_1=prxparse('/UNIT1A\s*$/i'); /*add \s* */
patternID_2=prxparse('/UNIT\s*$/i'); /*add \s* */
patternID_3=prxparse('/cat\s*$/i'); /*add \s* */
end;
b=prxmatch(patternID_1, a);
c=prxmatch(patternID_2, a);
d=prxmatch(patternID_3, a);
run;
proc print data=test2;
run;
or
data test2;
set test1;
retain patternID_1 patternID_2 patternID_3;
if _n_=1 then do;
patternID_1=prxparse('/UNIT1A$/i');
patternID_2=prxparse('/UNIT$/i');
patternID_3=prxparse('/cat$/i');
end;
b=prxmatch(patternID_1, trim(a)); /*add trim(a) */
c=prxmatch(patternID_2, trim(a)); /*add trim(a) */
d=prxmatch(patternID_3, trim(a)); /*add trim(a) */
run;
proc print data=test2;
run;
SAS strings are fixed-length.
Once the length for A is determined in a data step, it cannot change.
So if the value you save does not use all the length, the rest of the string contains spaces.
Looks like it is doing what you asked.
3015 data test1;
3016 do a='there is a cat','IT IS UNIT1A','IT IS unit' ;
3017 b = prxmatch('/UNIT1A$/i',a);
3018 c = prxmatch('/UNIT$/i',a);
3019 d = prxmatch('/cat$/i',a);
3020 put (_all_) (=);
3021 output;
3022 end;
3023 run;
a=there is a cat b=0 c=0 d=12
a=IT IS UNIT1A b=0 c=0 d=0
a=IT IS unit b=0 c=0 d=0
Did you want to trim the value of A?
3042 data test1; 3043 do a='there is a cat','IT IS UNIT1A','IT IS unit' ; 3044 b = prxmatch('/UNIT1A$/i',trim(a)); 3045 c = prxmatch('/UNIT$/i',trim(a)); 3046 d = prxmatch('/cat$/i',trim(a)); 3047 put (_all_) (=); 3048 output; 3049 end; 3050 run; a=there is a cat b=0 c=0 d=12 a=IT IS UNIT1A b=7 c=0 d=0 a=IT IS unit b=0 c=7 d=0
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.