Help using Base SAS procedures

Most popular value from a text string

Reply
New Contributor
Posts: 4

Most popular value from a text string


Hi,

I'm trying to pull out the most popular value from a text string and create a new variable that tells me what it is.

e.g.

'AAAABBBCCD' would give me a new variable with the value of 'A' in this instance.

Can anyone help please?

Thanks in advance.

Regular Contributor
Posts: 217

Re: Most popular value from a text string

Dear Miance,

I do not know how many rows of data you are working with; the length of the source variable; nor whether you want the new variable on the same row as the original variable.  Are the values in the variable limited to the 26 letters in the alphabet?

Super User
Super User
Posts: 7,401

Re: Most popular value from a text string

Please clarify the requirements.  There are many string functions, as can be found in the docs: http://support.sas.com/publishing/pubcat/chaps/59343.pdf

However its dependant on your specific scenario.  A simple calculation on the A or B or C etc. is relatively straight-forward, however its probably unlikely you want that.

Super Contributor
Posts: 282

Re: Most popular value from a text string

Hi,

The following gives "A":

data _null_;

  length want $1;

  have='AAAABBBCCD';

  text=have;

  max=0;

  do until(lengthn(text)=0);

    achar=substr(text,1,1);

    tally=countc(trim(text),achar);

    if tally gt max then

    do;

      want=achar;

      max=tally;

    end;

    text=compress(text,achar);

  end;

  put want=;

run;

Regards,

Amir.

Respected Advisor
Posts: 3,777

Re: Most popular value from a text string

I had the pretty much the same idea as you with slightly different functions.  I made a list for ties.

data _null_;
  
input s $char32.;
   ws=compress(s,
' '); *don't count spaces;
  
do while(not missing(ws));
      l=first(ws);
      c=countc(ws,l,
'TI'); *add I to ignore case;
      m=max(m,c);
      length p $32;
      p=ifc(c eq m,catt(p,l),p);
      ws=compress(ws,l,
'I'); *add I to ignore case;
     
end;
  
put 'NOTE: Most popular character(s) :' p 'frequency:' m;
   cards;
AA  aaBBBCCD
CcC  AA  AABBBCCD
lkdjLabnejndkijdidnd
   run;
New Contributor
Posts: 4

Re: Most popular value from a text string

Thanks very much Amir this works how I wanted it to.

Respected Advisor
Posts: 3,777

Re: Most popular value from a text string

I'm wondering why since gave you the program that "works how you wanted" that you did not mark the reply correct but merely helpful which leaves your question "Not Answered".

New Contributor
Posts: 4

Re: Most popular value from a text string

Updated.

Super User
Posts: 9,681

Re: Most popular value from a text string

data temp;
 set sashelp.class(keep=name);
 do i=1 to length(name);
  char=upcase(char(name,i));
  output;
 end;
 drop i;
run;
proc freq data=temp order=freq noprint;
by name;
tables char /out=temp1(drop=percent) nopercent ;
run;
data want;
 set temp1;
 by name;
 if first.name;
run;

Xia Keshan

New Contributor
Posts: 4

Re: Most popular value from a text string

Thanks all for your help, I have a solution that works now.

Ask a Question
Discussion stats
  • 9 replies
  • 319 views
  • 4 likes
  • 6 in conversation