BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aaou
Obsidian | Level 7

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

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
aaou
Obsidian | Level 7

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.

 

Reeza
Super User

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

 

 

Reeza
Super User

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. 

aaou
Obsidian | Level 7

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! 

Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1127 views
  • 4 likes
  • 3 in conversation