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 |
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,")");
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;
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.
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.
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
;
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));
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.