Solved
Contributor
Posts: 40

# Calculating percentages across row

I have a dataset which takes the following form:

 ID North East West South 4634 45 767 34 65 1264 23 54 643 43 2654 5 34 342 434

I want to calcualte the percentage across each row for each ID, so that I get something that looks like this:

 ID North East West South Total 4634 5% 84% 4% 7% 100% 1264 3% 7% 84% 6% 100% 2654 1% 4% 42% 53% 100%

Accepted Solutions
Solution
‎06-23-2016 01:31 AM
Posts: 1,163

## Re: Calculating percentages across row

[ Edited ]

data have;
input ID	North	East	West	South;
tot=sum(of North,East,	West,	South);
cards;
4634	45	767	34	65
1264	23	54	643	43
2654	5	34	342	434
;

data want;
set have;
array pct(5) North	East	West	South tot;
array pcts(5) North_	East_	West_	South_ tot_;
do i = 1 to 5;
pcts(i)=input((pct(i)/tot),PERCENT8.);
end;
run;
Thanks,
Jag

All Replies
Solution
‎06-23-2016 01:31 AM
Posts: 1,163

## Re: Calculating percentages across row

[ Edited ]

data have;
input ID	North	East	West	South;
tot=sum(of North,East,	West,	South);
cards;
4634	45	767	34	65
1264	23	54	643	43
2654	5	34	342	434
;

data want;
set have;
array pct(5) North	East	West	South tot;
array pcts(5) North_	East_	West_	South_ tot_;
do i = 1 to 5;
pcts(i)=input((pct(i)/tot),PERCENT8.);
end;
run;
Thanks,
Jag
Contributor
Posts: 40

## Re: Calculating percentages across row

It gives the following error:

ERROR 22-322: Syntax error, expecting one of the following: ), -.

ERROR 200-322: The symbol is not recognized and will be ignored.

Super User
Posts: 24,016

## Re: Calculating percentages across row

Remove total out of the array and use sum(of pct(*)) instead as the total.

Super User
Posts: 24,016

## Re: Calculating percentages across row

Your error shouldn't be caused by that though. In general when you get an error post your code and the error. We can't see your computer screen.

Contributor
Posts: 40

## Re: Calculating percentages across row

Hey thanks, for some reason it works now. Would you mind re-commenting with the amended code, with the suggestion you have made in the above comment....just so that I know what exactly you meant?

I'm sorry, I'm new to programing, so struggling a bit.

Thanks again!

Super User
Posts: 24,016

## Re: Calculating percentages across row

No, @Jagadishkatam original solution was correct. I missed seeing the tot calculation when he read the file.

Your error was likely caused by a typo. If you didn't change anything and it ran I would double check my log and results.

🔒 This topic is solved and locked.

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

Discussion stats
• 6 replies
• 372 views
• 4 likes
• 3 in conversation