BookmarkSubscribeRSS Feed
nkm1
Calcite | Level 5

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;
3 REPLIES 3
ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-02-23 à 14.44.29.png

 

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;

 

ChrisNZ
Tourmaline | Level 20

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.

Tom
Super User Tom
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 892 views
  • 3 likes
  • 4 in conversation