BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
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
 
10 REPLIES 10
ChrisNZ
Tourmaline | Level 20

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

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

@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.

 

PhilC
Rhodochrosite | Level 12
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.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
PhilC
Rhodochrosite | Level 12

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.  

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
PhilC
Rhodochrosite | Level 12

I stand corrected

Tom
Super User Tom
Super User

@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.

Tom
Super User Tom
Super User

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 6755 views
  • 5 likes
  • 6 in conversation