variant | Count |
---|---|
Altroz xe | 1 |
Altroz xm | 2 |
Grandi10 Magna | 2 |
Grandi20 Sport | 1 |
Swift Lxi | 1 |
Swift Vxi | 2 |
Tiago xm | 1 |
Tiago xz | 2 |
Venue Magna | 1 |
data dups;
input make : $ 20. variant $ 20. ;
datalines;
Tata Altroz xm
Tata Altroz xm
Tata Altroz xe
Tata Tiago xm
Tata Tiago xz
Tata Tiago xz
Suzuki Swift Lxi
Suzuki Swift Vxi
Suzuki Swift Vxi
Hyundai Grandi10 Magna
Hyundai Grandi10 Magna
Hyundai Grandi20 Sport
Hyundai Venue Magna
;
run;
proc freq data=dups ;
tables variant / out=count (rename=(Frequency=count)) nocol nocum norow nopercent ;
run;
I want rename Frequency to Count
Required Output
variant | Count |
---|---|
Altroz xe | 1 |
Altroz xm | 2 |
Grandi10 Magna | 2 |
Grandi20 Sport | 1 |
Swift Lxi | 1 |
Swift Vxi | 2 |
Tiago xm | 1 |
Tiago xz | 2 |
Venue Magna | 1 |
The rename option works as intended: the variable is renamed in the output data set.
You have 2 options to obtain the output you want:
1. Easy: Add option noprint to proc freg, and then use proc print.
2. Harder: Use proc template to modify the default behaviour of proc freq.
Step 1: LOOK AT the output data set named COUNT from PROC FREQ, with your own eyes. See what variable names are there. There is no variable named FREQUENCY, and so you can't rename variable FREQUENCY to anything else.
Step 2 (optional): You can rename the existing variables to something else, exactly as you tried to do.
@BrahmanandaRao wrote:
data dups; input make : $ 20. variant $ 20. ; datalines; Tata Altroz xm Tata Altroz xm Tata Altroz xe Tata Tiago xm Tata Tiago xz Tata Tiago xz Suzuki Swift Lxi Suzuki Swift Vxi Suzuki Swift Vxi Hyundai Grandi10 Magna Hyundai Grandi10 Magna Hyundai Grandi20 Sport Hyundai Venue Magna ; run; proc freq data=dups ; tables variant / out=count (rename=(Frequency=count)) nocol nocum norow nopercent ; run;
I want rename Frequency to Count
Required Output
variant Count Altroz xe 1 Altroz xm 2 Grandi10 Magna 2 Grandi20 Sport 1 Swift Lxi 1 Swift Vxi 2 Tiago xm 1 Tiago xz 2 Venue Magna 1
Required output where?
If you want results or report table then you can use another procedure to do simple counts
proc tabulate data=dups ; class variant; tables variant =' ', n='count' / box='variant'; run;
In Proc Tabulate you can override the default label for a variable or statistic with the ='something' immediately after the variable or statistic. Above the label of the n statistic which does a simple count is changed from the default n to count.
The label is suppressed for the variable variant and the "box" which is the upper left corner of the table has the text variant placed to look as desired.
Or Proc Report:
Proc report data=dups; column variant n; define variant / group; define n /'Count' ; run;
Again n is a statistic. The define statement for n lets you set a label or other appearance options.
To get the count of variant as described then roll for the variable is set to group so all the like values are counted together.
proc sql;
select distinct variant, count(*) as count
from dups
group by variant;
run;
I find I use PROC SQL in place of PROC FREQ when I need this simple level of control of output.
@PhilC wrote:
proc sql; select distinct variant, count(*) as count from dups group by variant; run;
I find I use PROC SQL in place of PROC FREQ when I need this simple level of control of output.
While I agree with your point in some cases, in this specific case it doesn't help. PROC FREQ automatically names the output variable to be COUNT, which seems to be what the request from the original post was. And of course, renaming the variable in the PROC FREQ output is trivial, if you do it right, and just as easy as your SQL code.
Ok, this got me. The ODS output names the column "Frequency", but the SAS data set creates a column called "COUNT". I believe the requester wants an ODS table that creates a column named COUNT. I could be wrong.
@PhilC wrote:
proc sql; select distinct variant, count(*) as count from dups group by variant; run;
So the syntax "as count" here is redundant. Thanks Miller.
@PhilC wrote:
Ok, this got me. The ODS output names the column "Frequency", but the SAS data set creates a column called "COUNT". I believe the requester wants an ODS table that creates a column named COUNT. I could be wrong.
@PhilC wrote:
proc sql; select distinct variant, count(*) as count from dups group by variant; run;
So the syntax "as count" here is redundant. Thanks Miller.
No, if you are using SQL, you do need count(*) as count, otherwise the variable created gets a default variable name.
If you are in PROC FREQ and use the OUT= option, you don't need to rename anything to COUNT.
I stand corrected
@PhilC wrote:
Ok, this got me. The ODS output names the column "Frequency", but the SAS data set creates a column called "COUNT". I believe the requester wants an ODS table that creates a column named COUNT. I could be wrong.
@PhilC wrote:
proc sql; select distinct variant, count(*) as count from dups group by variant; run;
So the syntax "as count" here is redundant. Thanks Miller.
In general you will find that using the output dataset that a procedure's syntax produces directly will work much better than trying to capture the output intended for a printed report as a dataset by using the ODS OUTPUT statement. Much like the difference between importing data from a database instead of trying to convert an Excel based report into a dataset.
If you do not give the SQL column either a name or a label then the printout will have blanks for the column header and if you did include it into a CREATE TABLE statement without giving it a name then it will get a goofy name like _TEMG001.
Huh? The variable that contains the frequencies is already named COUNT.
Did you want to rename it from count to frequency?
proc freq data=dups ;
tables variant / out=count (rename=(count=Frequency)) nocol nocum norow nopercent ;
run;
proc print data=count;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.