BookmarkSubscribeRSS Feed
twildone
Pyrite | Level 9

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

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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,")");

slchen
Lapis Lazuli | Level 10

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

twildone
Pyrite | Level 9

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.

 

slchen
Lapis Lazuli | Level 10

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
;

MikeZdeb
Rhodochrosite | Level 12

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));

 

ChrisNZ
Tourmaline | Level 20

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;

 

Patrick
Opal | Level 21

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1668 views
  • 0 likes
  • 6 in conversation