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!
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.
Please show us some sample data and the desired output
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.
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;
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 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.
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.