DATA Step, Macro, Functions and more

re: Insert Spaces

Reply
Regular Contributor
Posts: 229

re: Insert Spaces

Hi....I would like to insert spaces between the number and units in a dataset that has different strengths. The data can be entered in different forms and I am wondering if there is a simple way to do this...Thanks.

 

Have

Want

0.1mg

0.1 mg

.3ml

0.3 ml

1.2mcg

1.2 mcg

1.5g

1.5 g

0.1mg/g

0.1 mg / g

.3ml/mg

0.3 ml / mg

1.2mcg/.4g

1.2 mcg / 0.4 g

1.5g/0.3kg

1.5 g / 0.3 kg

Super User
Super User
Posts: 7,951

Re: re: Insert Spaces

Hi,

 

There is no simple suggestion here.  The reason is that you are dealing with a number of different connotations and outcomes.  Its not a simple logic problem.  For instance you can tranwrd / with that character and a space each side.  Search for the first alphabetic character and then insert a space, but you have several other bits to condsider.  I would check where this data is coming in from, in most standard structures (CDISC for example), the unit would be in a separate variable (this is the easiest way, as then you would have a code list), or within delimiters so you can easily pull it out.  

 

I think your best bet is to go the way of creating a distinct list of units and then parsing that out from the result:

data units;

  unit='mg';

  unit='ml';

run;

data _null_;

  set units;

  call execute('data want;  

                         set have;  

                         length unit $50;

                         if index(have,"'||strip(unit)||'")>0 then do;

                            unit="'||strip(unit)||'";

                            have=tranwd(have,"'||strip(unit)||'","");

                         end;

                        run;');

run;

 

The idea here is that for each unique unit, your data is checked, if that is found then the new unit variable takes that variable and then it is removed from the intial data.  Once this has been done for all units you can then look concatenating back for a full string or putting in brackets/adding spacing, e.g. param=cats(have," (",unit,")");

Super Contributor
Posts: 275

Re: re: Insert Spaces

data Have;
input unit $10.;
new_unit=prxchange('s/([\/])/ $1 /',-1,prxchange('s/(?<=\d)(\w+?)/ $1/',-1,unit));
cards;
0.1mg
.3ml
1.2mcg
1.5g
0.1mg/g
.3ml/mg
1.2mcg/.4g
1.5g/0.3kg
;
run;

Super User
Super User
Posts: 7,951

Re: re: Insert Spaces

Unfortunately, you have hit one of the problems I mentioned in my post, your output second to last row looks like:

1.2 mcg / .4 g

Not the 1.2 mcg / 0.4 g given in the example.  

Regular Contributor
Posts: 229

Re: re: Insert Spaces

Hi Slchen.....thank you for your help and suggestion. I ran the code with your suggestion and just notice that when the strength would be, for example, 650MG, the result would be 6 5 0 MG. It would separated the numerical values. Can that be prevented?...Thanks.

 

Super Contributor
Posts: 275

Re: re: Insert Spaces

data Have;
input unit $20.;
new_unit=prxchange('s/(^|\s)(?=\.)/$1 0/',-1,prxchange('s/([\/])/ $1 /i',-1,prxchange('s/(d?.\d+)(?=\w)/$1 /',-1,unit)));
cards;
0.344g
0.1mg
.3ml
1.21mcg
1.5g
.456mg/.46ml
0.1mg/g
.378ml/mg
1.2mcg/.4g
1.54mg/0.3kg
10ml
650MG
1.33mg/g
10.663mg/ML
;

Valued Guide
Posts: 765

Re: re: Insert Spaces

Very nice.

 

I think that there are a couple of extra spaces (leading and within) to get rid of so maybe add ...

 

new_unit=prxchange('s/(^|\s)(?=\.)/$1 0/',-1,prxchange('s/([\/])/ $1 /i',-1,prxchange('s/(d?.\d+)(?=\w)/$1 /',-1,unit)));

 

new_unit=compbl(left(new_unit));

 

PROC Star
Posts: 1,760

Re: re: Insert Spaces

Only 2 prxparse and easier to read imho.

 

data HAVE;
input UNIT $20.;
%* add spaces;
UNIT1=compbl(prxchange('s/^([\d.]+)([a-z]+)(\/)?([.\d]*)([a-z]*)/$1 $2 $3 $4 $5/i',1,UNIT));
%* add zeroes before dots if applicable;
UNIT2=prxchange('s/(?:\B(\.))/0$1/i',-1,UNIT1);
put UNIT1 $20. UNIT2 $20.;
cards;
0.344g
0.1mg
.3ml
1.21mcg
1.5g
.456mg/.46ml
0.1mg/g
.378ml/mg
.2mcg/.4g
1.54mg/0.3kg
10ml
650MG
1.33mg/g
10.663mg/ML
run;

 

Respected Advisor
Posts: 4,173

Re: re: Insert Spaces

[ Edited ]

Another variant using RegEx:

 

data Have;
input unit $10.;
length new_unit $20;
new_unit=prxchange('s/([^\d\.\s]+)/ \1/o',-1,unit);
new_unit=prxchange('s/(\s*\/+\s*)/ \/ /o',-1,new_unit);
cards;
0.1mg
.3ml
1.2mcg
1.5g
0.1mg/g
.3ml/mg
1.2mcg/ .4g
1.5g/0.3kg
;
run;

Ask a Question
Discussion stats
  • 8 replies
  • 302 views
  • 0 likes
  • 6 in conversation