BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
znhnm
Quartz | Level 8

Hi, I have a column in my dataset called number_of_items. I want to calculated the percentages of the items. e.g. 100 * (first record of number_of_items) / sum of number_of_items. How do I do this calculation? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you need a data set for further manipulation or a report that people read?

 

Best is to provide some example data, or reference one of the SAS supplied data sets such as SASHELP.Class, and walk us through details. The way you say "(first record of number_of_items) / sum of number_of_items" raises lots of concerns about exactly what the output is supposed to look like. With percentages I always want a very clear description of how the total numerator and denominator of any given percentage are obtained.

 

 

By the way, did YOU calculate the number_of_items variable? I ask because it may be that step isn't needed depending on exactly what you expect.

 

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Please show us some sample data and the desired output

--
Paige Miller
znhnm
Quartz | Level 8
Hi, I thank you. I provided a sample dataset on the under the other comment. I don't know how to provide an example desired output.
ballardw
Super User

Do you need a data set for further manipulation or a report that people read?

 

Best is to provide some example data, or reference one of the SAS supplied data sets such as SASHELP.Class, and walk us through details. The way you say "(first record of number_of_items) / sum of number_of_items" raises lots of concerns about exactly what the output is supposed to look like. With percentages I always want a very clear description of how the total numerator and denominator of any given percentage are obtained.

 

 

By the way, did YOU calculate the number_of_items variable? I ask because it may be that step isn't needed depending on exactly what you expect.

 

znhnm
Quartz | Level 8

Hi, thanks for the reply.

A similar dataset looks like this:

data example;
do bin = 1 to 3;
number_of_items = round(rand('uniform')*100);
output;
end;
 

I want a 3rd column to calculate the percentages.

run;

ballardw
Super User

Instead of a random number lets create a fixed set so we can discuss the actual calculation.

 

data have;
  input bin   number_of_items;
datalines;
1  51
2  20
3  49
;

Is this what you are looking for? The Pctn is displayed to two decimals by default. That can be changed with options.

proc tabulate data=have;
   class bin;
   freq number_of_items;
   table bin,
         pctn
   ;
run;

The Freq option makes the procedure treat the data as if there are "number_of_items" identical rows of data so can be used as the N of a statistic.

znhnm
Quartz | Level 8
Exactly! The calculation is correct! Thanks a lot! However, can I add tis pctn column in my dataset as the 3rd column? I also have anoter column called number_of_items and I want to calculate the pctn2 as the 4th colummn in my current dataset
ballardw
Super User

@znhnm wrote:
Exactly! The calculation is correct! Thanks a lot! However, can I add tis pctn column in my dataset as the 3rd column? I also have anoter column called number_of_items and I want to calculate the pctn2 as the 4th colummn in my current dataset

Now you have to provide actual example data. Typically this sound like calculate and then recombine with your existing data.

Proc Tabulate will create output data sets but it takes a bit to get used to the content. Add an OUT=<datasetname of choice> to the Proc tabulate statement and you can then look at what one looks like. My example would create a pretty simple data set but there will will be variables you don't want related to more complex output possible with the procedure.

If your other data isn't too ugly it should be possible to MERGE with a data step or join using proc sql.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 844 views
  • 2 likes
  • 3 in conversation