break up character string into differen letters

Reply
Occasional Contributor
Posts: 19

break up character string into differen letters

Hi

How would I break up a character string to give the individual letters in sas. tried a transtrn, translate but these are not working.

Please assist.

Respected Advisor
Posts: 3,777

Re: break up character string into differen letters

Maybe you should look at SUBSTR function.

Super User
Posts: 17,842

Re: break up character string into differen letters

why?

Super User
Posts: 5,257

Re: break up character string into differen letters

Do you mean to have each letter in a separate variable?

You could achieve this using a do loop in conjunction with substr, and perhaps store the result in an array (depending on what you intend to do with letters later on.

May I ask about the underlying requirement for this?

Data never sleeps
Occasional Contributor
Posts: 19

Re: break up character string into differen letters

I would like to convert characters to numeric e.g A= 01

Super User
Posts: 17,842

Re: break up character string into differen letters

So, CAT becomes C01?

What are the rules? transwrd will replace characters with other characters or numbers in this case and proc format can be useful for recoding observations.

Occasional Contributor
Posts: 19

Re: break up character string into differen letters

CAT = 030120

Super User
Super User
Posts: 7,407

Re: break up character string into differen letters

Update to the below (note that a and A are different, I assumed all upper case, hence the -64):

data want (drop=i);

  myvar="CAT";

  attrib newvar format=$200.;

  do i=1 to length(myvar);

    newvar=strip(newvar)||strip(put(rank(substr(myvar,i,1))-64,z2.));

  end;

run;

---

A simplistic approach - one tranwrd for each letter.  There is a better way by converting to ascii number which I will look at in a mo.

data want;

  myvar="CAT";

  attrib newvar format=$200.;

  newvar=tranwrd(myvar,"A","01");

  newvar=tranwrd(newvar,"B","02");

  newvar=tranwrd(newvar,"C","03");

  /* ... */

  newvar=tranwrd(newvar,"T","20");

  /* ... */

run;

Super User
Posts: 5,257

Re: break up character string into differen letters

Still, why?

Data never sleeps
Occasional Contributor
Posts: 19

Re: break up character string into differen letters

Hi Linus

I am trying to convert all letter characters into a numbers e.g EEE12345C.

I need to do this for mulitple accounts

Super User
Posts: 6,946

Re: break up character string into differen letters

How about this;

%let base=%eval(%sysfunc(rank(A))-1);

/* This takes care of the collating sequence (ASCII or EBCDIC) */

data want;

length string2 $200;

set have; *reads string1;

string2 = '';

do x1 = 1 to length(string1);

  x2 = rank(substr(string1,x1,1));

  if &base < x2 <= &base+26

  then string2 = trim(string2) !! put(x2-&base,z2.);

  else string2 = trim(string2) !! substr(string1,x1,1);

end;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: break up character string into differen letters

You can try something like this:

data test;

    input id $20.;

cards;

CAT

EEE12345C

;

data _null_;

    do i=65 to 90;

        num=put(i-64,Z2.);

        chc=byte(i);

        call symputx(chc,num);

        output;

    end;

run;

data want;

    set test;

    id=resolve(prxchange("s/([A-Z])/&\1./",-1,id));

run;

Ask a Question
Discussion stats
  • 11 replies
  • 442 views
  • 0 likes
  • 7 in conversation