DATA Step, Macro, Functions and more

Calculating percentages across row

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Calculating percentages across row

I have a dataset which takes the following form:

 

 

IDNorthEastWestSouth
4634457673465
1264235464343
2654534342

434

 

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

IDNorthEastWestSouthTotal
46345%84%4%7%100%
12643%7%84%6%100%
26541%4%42%53%100%

 

Please help! Thank you! 


Accepted Solutions
Solution
‎06-23-2016 01:31 AM
Trusted Advisor
Posts: 1,137

Re: Calculating percentages across row

[ Edited ]

Please try

 

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

View solution in original post


All Replies
Solution
‎06-23-2016 01:31 AM
Trusted Advisor
Posts: 1,137

Re: Calculating percentages across row

[ Edited ]

Please try

 

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

Posted in reply to Jagadishkatam

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: 19,851

Re: Calculating percentages across row

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

 

 

Super User
Posts: 19,851

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: 19,851

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.

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

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