BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ballakaay
Calcite | Level 5

Hello,

I have a variable named "fac_code" which contains code delimited by space. However, there are some errors in code where there is no delimiter between code. The code starts with a letter and followed by some digits. I would like to fix it by adding space between codes. For example,

 

fac_code

K111I001 C01

A01B001

 

I want it to be

K111 I001 C01

A01 B001

 

I did something like

data a;

set a;

if prxmatch('/[A-Z](\d+)[A-Z](\d+)/)', fac_code)>0 then

fac_code_clean = catx(' ', char(fac_code,2)) but it does not work

 

Any help is appreciated! Thanks in advance

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input x $80.;
cards;
K111I001 C01
A01B001
;

data want;
 set have;
 want=compbl(prxchange('s/([a-z]\d+)/\1 /i',-1,x));
run;

View solution in original post

6 REPLIES 6
AhmedAl_Attar
Ammonite | Level 13

Hi @ballakaay 

Try this

data have;
length fac_code $20; /* Declare a length that allows adding spaces */
fac_code='K111I001 C01'; output;
fac_code='A01B001'; output;
run;

data want;
set have;
fac_code=compress(fac_code); /* Remove existing spaces */
RegularExpressionId = prxparse('s/\w\d{2,}/ $0/');
call prxchange(RegularExpressionId, -1, fac_code); /* Insert delimiting space */
fac_code=strip(fac_code);
put fac_code=;
run;
Ksharp
Super User
data have;
input x $80.;
cards;
K111I001 C01
A01B001
;

data want;
 set have;
 want=compbl(prxchange('s/([a-z]\d+)/\1 /i',-1,x));
run;
ballakaay
Calcite | Level 5

Thank you so much! That is exactly what I want

ChrisNZ
Tourmaline | Level 20

One way:

data HAVE;
  CODE='K111I001 C01 '; output;
  CODE='A01B001      '; output;
run;

data WANT;
  set HAVE;
  CODE = prxchange('s/(\d)([A-Z]\d*)/\1 \2/', -1, CODE); 
  putlog CODE=;
run;
CODE=K111 I001 C01
CODE=A01 B001

 

Amir
PROC Star

Hi,

 

Whilst you have already been provided with a number of solutions, I have tried to break down each part of the regular expression in the comments in the below data step:

 

data have;
  infile datalines truncover;
  input fac_code $char50.;
  datalines;
K111I001 C01
A01B001
;


data want(drop = substitute);
  /* define perl regular expression for substitution (assigned once in data step) */
  substitute = cat(
                   's/'      /* substitution specification start:       */
                  ,'(\d)'    /* replace any digit                       */
                  ,'([A-Z])' /* that is followed by an uppercase letter */
                  ,'/'       /* with                                    */
                  ,'$1'      /* the digit that was found                */
                  ,' '       /* followed by a new space character       */
                  ,'$2'      /* and the uppercase letter that was found */
                  ,'/'       /* substitution specification end          */
                  ,'o'       /* compile-once (i.e., not every obs)      */
                  );
  
  /* use Do-Whitlock to loop through obs */
  do until(last_obs);
    set have end = last_obs;
  
    fac_code_clean = prxchange(substitute, -1, fac_code);
    
    output;
  end;
  
  /* prevent unnecessary return to start of data step */
  stop;
run;

 

Amir_0-1750001733567.png

 

 

 

 

Thanks & kind regards,

Amir.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 6 replies
  • 570 views
  • 1 like
  • 6 in conversation