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.
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?
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.
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.
I had the pretty much the same idea as you with slightly different functions. I made a list for ties.
Thanks very much Amir this works how I wanted it to.
Updated.
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
Thanks all for your help, I have a solution that works now.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.