1
votes

Is there a way through PROC FREQ to gather the frequencies of just the first letter of names? For example, I have a list of 50 names under the variable Name. I would like to take the frequencies of names by the first letter of the name. I have done this creating a new variable which takes a substring of the original name variable, and it works fine. But I am wondering if there is a way within PROC FREQ where I can do this without having to create a new variable? This is the code I have used:

DATA FirstNames;
   INFILE "(insert file)" firstobs=2;
   INPUT Name $ @@;
   Letter=substr(Name,1,1);
RUN;

proc freq data = FirstNames;
    Tables Letter;
run;
2

2 Answers

4
votes

Apply a format of length 1 to the variable.

proc freq data = FirstNames;
Tables Name;
Format Name $1.;
run;
1
votes

An easy way to do this using proc sql:

proc sql:
    select substr(name, 1, 1) as firstLetter, count(*)
    from FirstNames
    group by substr(name, 1, 1);