Learning SAS? Welcome to the exclusive online community for all SAS learners.

Add a special character after a digit in a character string

Reply
Occasional Contributor RRM
Occasional Contributor
Posts: 5

Add a special character after a digit in a character string

Hi everyone

I have some very messy data that i need to clean up. I have this one variable that has a bunch of results put together which i need to somehow separate. The variable looks something like this: abcd - 100efgh - 200ijkl, total - 300

I need it to look like this: abcd - 100; efgh - 200; ijkl, total - 300 or if it's easier separate in 3 variables. Every result is different, meaning there are more than 3 results in one with different formats, but that is the overall issue. I am thinkin the way to solve this is to add a semicolon if there is a letter after a digit. That seems to be the way that would work for all cases. I am just not sure how to do that. Or it it's easier separate it in different variables if there is a letter after a number. Any ideas on how to do that?

Thank you for your help

Super User
Super User
Posts: 7,988

Re: Add a special character after a digit in a character string

If its only three elements then find should be able to handle it:

data have;

  a="abcd - 100efgh - 200ijkl, total - 300";

run;

data want (rename=(a=var3));

  set have;

  var1=substr(a,1,findc(a,'a',findc(a,'-'),'a')-1);

  a=substr(a,findc(a,'a',findc(a,'-'),'a'));

  var2=substr(a,1,findc(a,'a',findc(a,'-'),'a')-1);

  a=substr(a,findc(a,'a',findc(a,'-'),'a'));

run;

Ask a Question
Discussion stats
  • 1 reply
  • 218 views
  • 0 likes
  • 2 in conversation