BookmarkSubscribeRSS Feed
bayzid
Obsidian | Level 7

I am trying add a leading and trailing space to a character variable using the following command.
caps='  '||caps;
caps=caps||'    ';

It is adding a leading space but not the trailing space.

15 REPLIES 15
Kurt_Bremser
Super User

Depending on the defined length (e.g. with a LENGTH statement) and the actual content, the variable will always have trailing blanks; strings in the variable are padded to the defined length with blanks.

Trailing blanks are usually not displayed, unless you force their display (e.g. by using the $CHAR format).

bayzid
Obsidian | Level 7

I have enough length in the variable. The source variable is 100 Char long and i am using the following

caps=put(GenericName,$upcase200.); /*Capitalise all letters and setting the length to 200*/
			caps = compbl(tranwrd(caps, ",", " ,")); /* Adding a space before a comma */
			caps = compbl(tranwrd(caps, ".", " ."));
			caps = compbl(tranwrd(caps, ";", " ;"));
			caps = compbl(tranwrd(caps, "&", " + "));
			*caps = compbl(tranwrd(caps, "/", " + "));
				caps=' '||caps;
				caps=caps||" ";

 

bayzid
Obsidian | Level 7

what is the solution then. I need to add a trailing space for text mining.

Kurt_Bremser
Super User

Since there are already trailing spaces, there is no need to add any. The number of trailing spaces is always (defined length of variable) minus (position of last non-blank character).

bayzid
Obsidian | Level 7

But my following text mining command is not considering the trailing space.

pattern_id=prxparse('/( DONEPEZIL | GALANTAMINE | RIVASTIGMINE | MEMANTINE )/i');
	start=1;
	stop=length(caps);
	call prxnext(pattern_id, start, stop, caps, position, length);
	if (position>0)then
		cm_dementia=substr(caps, position, length);
Tom
Super User Tom
Super User

@bayzid wrote:

But my following text mining command is not considering the trailing space.

pattern_id=prxparse('/( DONEPEZIL | GALANTAMINE | RIVASTIGMINE | MEMANTINE )/i');
	start=1;
	stop=length(caps);
	call prxnext(pattern_id, start, stop, caps, position, length);
	if (position>0)then
		cm_dementia=substr(caps, position, length);

I assume it is not matching because you told PRXNEXT() to NOT use any of the trailing spaces by setting STOP to the last non-space character in the variable.

bayzid
Obsidian | Level 7

Everything is working when I am adding a dot at the end of the space.
caps1=' '||caps1;
caps=caps1||" .";

s_lassen
Meteorite | Level 14

I think you should drop the leading and trailing blanks, and look for word boundaries instead - beginning of string or end of string is also a word boundary:

pattern_id=prxparse('/\b(DONEPEZIL|GALANTAMINE|RIVASTIGMINE|MEMANTINE)\b/i');
BayzidurRahman
Obsidian | Level 7

That will pick the keyword by itself or with another keywords. For example, "DONEPEZIL" and "DONEPEZIL/OTHER". But i do not want the "DONEPEZIL/OTHER" to be picked up.

Tom
Super User Tom
Super User

@BayzidurRahman wrote:

That will pick the keyword by itself or with another keywords. For example, "DONEPEZIL" and "DONEPEZIL/OTHER". But i do not want the "DONEPEZIL/OTHER" to be picked up.


Do not add the spaces to the VARIABLE.  Add them to the string you pass to the FUNCTION. Remember that means that the position that it found the start of the match is off by one because of the extra space.

pattern_id=prxparse('/ (DONEPEZIL|GALANTAMINE|RIVASTIGMINE|MEMANTINE) /i');

call prxnext(pattern_id, start, vlength(caps)+2 , ' '||caps||' ', position, length);
if (position>0) then cm_dementia=substr(caps, position-1, length);
Patrick
Opal | Level 21

It could also be done with a RegEx but in your case may-be just use the scan() function with a blank defined as the word delimiter.

data demo;
  infile datalines truncover;
  input str $100.;
  length cm_dementia $40;
  do _i=1 to countw(str,' ');
    cm_dementia=scan(str,_i,' ');
    if upcase(cm_dementia) in ('DONEPEZIL', 'GALANTAMINE', 'RIVASTIGMINE', 'MEMANTINE') then
    output;
  end;
datalines;
donepezil galantamine donepezil/other rivastigmine memantine
;
proc print data=demo;
run;

Patrick_0-1686890339267.png

 

 

originalshilaji
Calcite | Level 5

Your new variable caps will be padded with blanks up to the 200th byte, ensuring precise and consistent data storage.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 15 replies
  • 2484 views
  • 0 likes
  • 8 in conversation