Hi all. Hopefully an easy question, for a Friday afternoon. A data set I read in has strings of numbers that look like this, this is one column in text format.
31.8 (29.7-33.9)
This is, for example, a rate and the confidence interval. i want to add a space before and after the dash, to make it look like this
31.8 (29.7 - 33.9)
Is this easy to do? Say the variable is "rate_ci".
@geneshackman wrote:
Group Category rate low high rate_ci
Gender Male 31.4 28.5 34.4 31.4 (28.5-34.4)
Gender Female 31.4 29.1 33.7 31.4 (29.1-33.7)
Gender Female 31.5 24.4 38.6 31.5 (24.4-38.6)
Let's assume you are trying to show us what the dataset HAVE looks like. And that the last column in your listing is the variable name RATE_CI. So to create a new dataset named WANT with the corrected version of RATE_CI run code like this:
data want;
set have;
rate_ci = tranwrd(rate_ci,'-',' - ');
run;
Check the results and make sure that adding the two extra space characters didn't cause it to run out of room to store the full string. That is make sure the right parenthesis haven't disappeared in the new dataset.
Use the Tranwrd Function
data _null_;
n = "31.8 (29.7-33.9)";
nn = tranwrd(n, "-", " - ");
put n = / nn =;
run;
Result:
n=31.8 (29.7-33.9) nn=31.8 (29.7 - 33.9)
Please show a more representative example of your data.
@geneshackman wrote:
Ah, sorry, i didn't give enough information. It's not just this number, there are a bunch of numbers, all having the same format. I want to change them all.
@geneshackman wrote:
Group Category rate low high rate_ci
Gender Male 31.4 28.5 34.4 31.4 (28.5-34.4)
Gender Female 31.4 29.1 33.7 31.4 (29.1-33.7)
Gender Female 31.5 24.4 38.6 31.5 (24.4-38.6)
Let's assume you are trying to show us what the dataset HAVE looks like. And that the last column in your listing is the variable name RATE_CI. So to create a new dataset named WANT with the corrected version of RATE_CI run code like this:
data want;
set have;
rate_ci = tranwrd(rate_ci,'-',' - ');
run;
Check the results and make sure that adding the two extra space characters didn't cause it to run out of room to store the full string. That is make sure the right parenthesis haven't disappeared in the new dataset.
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.
Ready to level-up your skills? Choose your own adventure.