## Help with construction of code

Solved
Frequent Contributor
Posts: 96

# Help with construction of code

Hi Everyone,

I need help with the construction of a variable, 4firm_ratio. 4firm_ratio is calculated as the market share of largest four firms within each industry.  For example., in an industry if there are 10 firms, I need to select the largest four firms. Let's say the largest four firms are ABC, MNO, STU, and XYZ. I need to calculate the market share for each of these firms. Market share of firm ABC is calculated as  square of SALE of ABC divide by total industry SALES. After calculating the market share of the largest four firms in an industry, I need to add the market share of ABC, MNO, STU, and XYZ. The addition will be called as 4firm_ratio.

Accepted Solutions
Solution
‎04-27-2012 10:41 AM
SAS Super FREQ
Posts: 9,371

## Re: Help with construction of code

Hi,

You did not say whether you had detail data (sales for individual industries, firms,months, days, years) or summarized data (sales for industries and  firms summarized to the year level). This makes a difference to your approach to the problem. Another piece of information that will be useful to know is whether you need this information for just 1 year -- or are all the years for a firm being summarized together?

Let's look at the information you need. You gave this as an example of what you need:

Market share of firm ABC is calculated as  square of SALE of ABC divide by total industry SALES.

So this one sentence implies to me that you need to do some summarizing by industry and by firm. Before you can calculate market share. There are many SAS procedures you could use to get total and individual sales. For example, PROC MEANS with a CLASS statement would give you sales for all the possible combinations of the CLASS variables, it can also give you the overall sales for the entire data set you send to it or it can give you the summary for each of the CLASS variables. On the other hand, if you are more comfortable with SQL, then you could use an SQL query (PROC SQL) in order to group and summarize your data.

Once you have the data summarized to the levels you need, then you can find the top 4 firms in each industry and can compute your ratios for the 4 firms in each industry.

It seems to me that you are starting with a word problem. In order to approach the solution to your word problem, my recommendation would be for you to translate your word problem into the types of tasks that need to be done and then try to figure out how to do a particular task with SAS. Your word problem is a very high level description. I would expect that the actual solution will depend on writing some code in order to calculate the summaries and variables that you want for just 1 industry -- and once you have that program working correctly, you can generalize the program to work for multiple industries.

The first part of that is understanding the structure of your data -- what does the data look like, what is the name of the SAS dataset, how are the data structured, which variables are character variables and which variables are numeric. Then figure out which SAS procedures or DATA step program techniques you need to solve your problem.

You did not say anything about your level of experience with SAS. Which SAS procedures do you know? Do you know DATA step programming? Do you know SAS Macro programming? Do you know SQL? Do you know other programming languages? Were you given any guidance about the data or type of program you needed to write?

Then, in what form do you need your output or results? Do you need a report? If you need a report, what form of report do you need? An HTML file, an RTF file, a PDF file? Do you need a SAS dataset or table as output? If so, what variables do you need to have in the output dataset? Do you need some other type of file as output (such as a CSV file)?

I realize this isn't probably the answer you were hoping for. In order for anyone to help you with code you will need to share more information and even the code that you've started with or tried so far. Showing a small subset or sample of your data is also useful. Otherwise, without any idea of what your data looks like or the code you've tried so far, it's nearly impossible for anyone to offer suggestions about how to solve the problem.

cynthia

All Replies
Frequent Contributor
Posts: 96

## Re: Help with construction of code

To construct the 4firm_ratio, I have the following information:

TIC= firm ticker which indentifies each firm

SIC= which identifies each industry

SALE= sales for each firm

YEAR= fiscal year for each firm

Solution
‎04-27-2012 10:41 AM
SAS Super FREQ
Posts: 9,371

## Re: Help with construction of code

Hi,

You did not say whether you had detail data (sales for individual industries, firms,months, days, years) or summarized data (sales for industries and  firms summarized to the year level). This makes a difference to your approach to the problem. Another piece of information that will be useful to know is whether you need this information for just 1 year -- or are all the years for a firm being summarized together?

Let's look at the information you need. You gave this as an example of what you need:

Market share of firm ABC is calculated as  square of SALE of ABC divide by total industry SALES.

So this one sentence implies to me that you need to do some summarizing by industry and by firm. Before you can calculate market share. There are many SAS procedures you could use to get total and individual sales. For example, PROC MEANS with a CLASS statement would give you sales for all the possible combinations of the CLASS variables, it can also give you the overall sales for the entire data set you send to it or it can give you the summary for each of the CLASS variables. On the other hand, if you are more comfortable with SQL, then you could use an SQL query (PROC SQL) in order to group and summarize your data.

Once you have the data summarized to the levels you need, then you can find the top 4 firms in each industry and can compute your ratios for the 4 firms in each industry.

It seems to me that you are starting with a word problem. In order to approach the solution to your word problem, my recommendation would be for you to translate your word problem into the types of tasks that need to be done and then try to figure out how to do a particular task with SAS. Your word problem is a very high level description. I would expect that the actual solution will depend on writing some code in order to calculate the summaries and variables that you want for just 1 industry -- and once you have that program working correctly, you can generalize the program to work for multiple industries.

The first part of that is understanding the structure of your data -- what does the data look like, what is the name of the SAS dataset, how are the data structured, which variables are character variables and which variables are numeric. Then figure out which SAS procedures or DATA step program techniques you need to solve your problem.

You did not say anything about your level of experience with SAS. Which SAS procedures do you know? Do you know DATA step programming? Do you know SAS Macro programming? Do you know SQL? Do you know other programming languages? Were you given any guidance about the data or type of program you needed to write?

Then, in what form do you need your output or results? Do you need a report? If you need a report, what form of report do you need? An HTML file, an RTF file, a PDF file? Do you need a SAS dataset or table as output? If so, what variables do you need to have in the output dataset? Do you need some other type of file as output (such as a CSV file)?

I realize this isn't probably the answer you were hoping for. In order for anyone to help you with code you will need to share more information and even the code that you've started with or tried so far. Showing a small subset or sample of your data is also useful. Otherwise, without any idea of what your data looks like or the code you've tried so far, it's nearly impossible for anyone to offer suggestions about how to solve the problem.

cynthia

Frequent Contributor
Posts: 96

## Re: Help with construction of code

Posted in reply to Cynthia_sas

Thanks a lot Cynthia.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 3 replies
• 299 views
• 1 like
• 2 in conversation