BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

dear all 

i have to count the frequency of the response type for each company in the data set and to prepare a separate table with company name and frequency of response type. 

the sample of the data and required output is given below

 

The sample data

Company Nameresponse type
3I Infotech Ltd.Independent
3I Infotech Ltd.Non-independent
3I Infotech Ltd.Non-independent
3I Infotech Ltd.Independent
3I Infotech Ltd.Non-independent
3I Infotech Ltd.Non-independent
3I Infotech Ltd.Non-independent
3M India Ltd.Independent
3M India Ltd.Independent
3M India Ltd.Independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3M India Ltd.Non-independent
3P Land Holdings Ltd.Non-independent
3P Land Holdings Ltd.Non-independent
3P Land Holdings Ltd.Independent
3P Land Holdings Ltd.Independent
3P Land Holdings Ltd.Independent
3P Land Holdings Ltd.Non-independent
3P Land Holdings Ltd.Non-independent
52 Weeks Entertainment Ltd.Non-independent
52 Weeks Entertainment Ltd.Non-independent
52 Weeks Entertainment Ltd.Non-independent
52 Weeks Entertainment Ltd.Non-independent
52 Weeks Entertainment Ltd.Independent
52 Weeks Entertainment Ltd.Independent
52 Weeks Entertainment Ltd.Independent
52 Weeks Entertainment Ltd.Independent

 

 

 

 

 

required output table

Company Nametotal FrequencyNon-independentIndependent
3I Infotech Ltd.752
3M India Ltd.1073
3P Land Holdings Ltd.743

 

Please suggest me SAS code perform the above discussed function

thanks in advance. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
	infile datalines dlm="09"x;
	input Company_Name:$50.	response_type:$50.;
	datalines;
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
;
run;

proc sql;
select Company_Name,count(response_type) as total,
       sum(response_type='Non-independent') as Nonindependent,
       sum(response_type='Independent') as Independent
   from have
    group by Company_Name;
quit;

View solution in original post

9 REPLIES 9
ed_sas_member
Meteorite | Level 14

Hi @srikanthyadav44 

Here is an approach to achieve this:

data have;
	infile datalines dlm="09"x;
	input Company_Name:$50.	response_type:$50.;
	datalines;
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
;
run;

/* Frequencies per Company_Name and response_type */
proc freq data=have noprint;
	table response_type / out=have_stat (drop=percent);
	by Company_Name;
run;

proc transpose data=have_stat out=have_stat_tr (drop=_:);
	var count;
	id response_type;
	by Company_Name;
run;

/* Frequencies per Company_Name */

proc freq data=have noprint;
	table Company_Name / out=have_stat_total (drop=percent rename=(count = total_Frequency));
run;

/* Final table */

data want;
	merge have_stat_total have_stat_tr;
	by Company_Name;
run;

Should you need only a report, you can use a proc tabulate for example:

proc tabulate data=have;
	class Company_Name response_type;
	table Company_Name, response_type all*n;
run;

All the best,

 

Ksharp
Super User
data have;
	infile datalines dlm="09"x;
	input Company_Name:$50.	response_type:$50.;
	datalines;
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3I Infotech Ltd.	Non-independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3M India Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Independent
3P Land Holdings Ltd.	Non-independent
3P Land Holdings Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Non-independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
52 Weeks Entertainment Ltd.	Independent
;
run;

proc sql;
select Company_Name,count(response_type) as total,
       sum(response_type='Non-independent') as Nonindependent,
       sum(response_type='Independent') as Independent
   from have
    group by Company_Name;
quit;
srikanthyadav44
Quartz | Level 8

thank you Mr.  

the SAS code suggested by you is working excellently. 

can you please provide me the code which can save the output table into a separate excel file

thanks in advance.  

Ksharp
Super User

1) Create a table like :

proc sql;

create table want as

........

 

and using PROC EXPORT to export output into an EXCEL file.

proc export data=want outfile='c:\temp\want.xlsx' dbms=excel replace;

run;

 

2) Try ODS EXCEL like:

ods excel file='c:\temp\want.xlsx' ;

proc sql;

select ..........

quit;

ods excel close;

srikanthyadav44
Quartz | Level 8

Dear Mr. 

I tried the two alternatives as suggested by you, but they are not working. 

output table is displayed in SAS,but not exported as excel file. 

 

please suggest me a complete SAS code include the function to create the table. because i may be wrong in arranging the code properly. 

 

thanks in advance 

srikanthyadav44
Quartz | Level 8

dear Mr. Ksharp

 

i also want to extent this function to get the frequency count for multiple years of data. 

the new data set format is as follows. the data set consists of the observations from 2001 to 2018. Almost all the companies will be repeated in each year. 

 

Company Nameyearresponse_type
8K Miles Software Services Ltd.2001Non-independent
8K Miles Software Services Ltd.2001Non-independent
8K Miles Software Services Ltd.2001Non-independent
8K Miles Software Services Ltd.2001Non-independent
8K Miles Software Services Ltd.2001Non-independent
Aarvi Encon Ltd.2001Non-independent
Aarvi Encon Ltd.2001Non-independent
Akar Auto Inds. Ltd.2001Non-independent
Akar Auto Inds. Ltd.2001Non-independent
Akar Auto Inds. Ltd.2001Non-independent
Akar Auto Inds. Ltd.2001Non-independent
Akar Auto Inds. Ltd.2001Non-independent
Alang Industrial Gases Ltd.2001Non-independent
Allied Digital Services Ltd.2001Non-independent
Amarjothi Spinning Mills Ltd.2001Non-independent
Amarjothi Spinning Mills Ltd.2001Non-independent
Ansal Buildwell Ltd.2001Non-independent
Ansal Buildwell Ltd.2001Non-independent
Ansal Buildwell Ltd.2001Non-independent
Ansal Buildwell Ltd.2001Non-independent
Ansal Buildwell Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apar Industries Ltd.2001Non-independent
Apex Buildsys Ltd.2001Non-independent
Apollo Hospitals Enterprise Ltd.2001Non-independent
Apollo Hospitals Enterprise Ltd.2001Non-independent
Apollo Hospitals Enterprise Ltd.2001Independent
Apollo Hospitals Enterprise Ltd.2001Independent
Apollo Hospitals Enterprise Ltd.2001Independent
Apollo Hospitals Enterprise Ltd.2001Independent
Apollo Hospitals Enterprise Ltd.2001Non-independent
Apollo Hospitals Enterprise Ltd.2001Independent

 

the required format the of the table is 

Company Nameyeartotal FrequencyNon-independentIndependent
3I Infotech Ltd.2001752
3M India Ltd.20011073
3P Land Holdings Ltd.2001743
.....
.....
.....
. ...
.....

 

 

please let me know how the modify the SAS code to make it suitable for the new format of the dataset. 

and also, how to export the output table. 

thanks in advance. 

Ksharp
Super User
proc sql;
create table want as
select Company_Name,year,count(response_type) as total,
       sum(response_type='Non-independent') as Nonindependent,
       sum(response_type='Independent') as Independent
   from have
    group by Company_Name,year;
quit;
proc export data=want outfile="/courses/d8fb3215ba27fe300/want.xlsx" dbms=xlsx replace;
run;
srikanthyadav44
Quartz | Level 8
thanks a lot. it is working
srikanthyadav44
Quartz | Level 8
thanks. it is working

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1052 views
  • 4 likes
  • 3 in conversation