SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
geneshackman
Pyrite | Level 9

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".

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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.

View solution in original post

16 REPLIES 16
PeterClemmensen
Tourmaline | Level 20

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)

 

geneshackman
Pyrite | Level 9
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.
Reeza
Super User

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
Pyrite | Level 9
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)
geneshackman
Pyrite | Level 9
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)
Reeza
Super User
How did you generate this table? Did you use a macro or data step? I'm guessing going back and fixing the code that generated this table is the right place to fix the issue and would be as simple as adding spaces in the CATX function most likely.
geneshackman
Pyrite | Level 9
I'm reading in a data set someone else created.
Tom
Super User Tom
Super User

@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.

geneshackman
Pyrite | Level 9
Thanks!
geneshackman
Pyrite | Level 9
sorry, i can't seem to get it to show up with the columns lined up.
Reeza
Super User
Use the code block to ensure your data shows up as needed or ideally create a data step as noted here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
geneshackman
Pyrite | Level 9
Thanks very much! This was what I wanted.
JosvanderVelden
SAS Super FREQ
Yes it's easy. You could for replace the '-' with ' - '. There are several ways to do that. You can use sas functions or if you prefer you can use regular expressions. One example is in the thread https://communities.sas.com/t5/SAS-Programming/Find-And-Replace-within-a-string/td-p/45104.
Best regards, Jos
geneshackman
Pyrite | Level 9
Hi. Sorry to say I can't follow the example in the thread. How would I use a sas function? Thanks.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 16 replies
  • 3168 views
  • 6 likes
  • 6 in conversation