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

Hi All,

 

I have a base dataset on which I am performing computation as: 

 

data one;
input name$ sal;
datalines;
A 100
B 200
C 300
D 400
E 500
;
run;

 

Now, I want to calculate as:

 

data two;
set one;
array x(*) sal;
do i = 1 to dim(x);
mypercentage = x{i}*100/[CUMULATIVE TOTAL OF SAL FROM DATASET ONE];
end;
run;
proc print;
run;

 

My issue is: I am able to run this dataset two and getting proper result if I am putting 2( just a random number) instead of the CUMULATIVE TOTAL OF SAL FROM DATASET ONE. 

 

I want to use the cumulative total of sal from dataset one and use it in dataset two which will tell like 100 is 6.66 percent of 1500 (cumulative total of sal from dataset one) and so on for every iteration.

 

Please suggest, how is it possible. I hope, I am clear with my question.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You might want a report procedure as the kind of percentages you are using can lead to confusion when used as input into other processes.

Please look at

proc tabulate data=one;
   class name;
   var sal;
   table name all=total,
         sal*(sum='Sal total' colpctsum="Percent of column total");
run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Your question is relatively clear, thanks for posting for sample data and the code you have so far!

A+ 😉

 

So, here's how to do what you want, though I'm not sure what your array is trying to do. I think you're trying to calculate the percent of total, so I'm going with that. If not, please clarify. 

 

data one;
input name$ sal;
datalines;
A 100
B 200
C 300
D 400
E 500
;
run;

proc means data=one noprint nway;
output out=tot_val sum(sal)=tot_sal;
run;

data two;
set one;
if _n_ = 1 then set tot_val (drop = _type_ _freq_);
percent = sal/tot_sal;
format percent percent12.1;
run;

proc print data=two;
run;

And an alternative method, if I guessed correctly:

*Another way without the multiple steps;
proc freq data=one;
table name/out=three outcum outpct;
weight sal;
run;

 

 

 

GurmeetKaur_23
Calcite | Level 5

Hi There,

 
Thanks for reply 🙂 but I want something that I have shown in the excel sheet and that too using arrays.
 
Can you help me resolve that issue via using arrays please?

How I want the output.PNG
Reeza
Super User

@GurmeetKaur_23 wrote:

Hi There,

 
 
Can you help me resolve that issue via using arrays please?

The results are similar to what you have in Excel, which part of the code/results suggested doesn't work for what you want? 

 

No, you can't use arrays. In SAS arrays work on a single row, not on columns. They're considered a shortcut reference to variables but not an array in the sense of matrix/arrays. If you really want to use arrays, you'll need to use IML and matrix language. 

 

ballardw
Super User

You might want a report procedure as the kind of percentages you are using can lead to confusion when used as input into other processes.

Please look at

proc tabulate data=one;
   class name;
   var sal;
   table name all=total,
         sal*(sum='Sal total' colpctsum="Percent of column total");
run;
GurmeetKaur_23
Calcite | Level 5

Thank you ballardw. Thanks a lot for helping me with the fact that "No, you can't use arrays. In SAS arrays work on a single row, not on columns. They're considered a shortcut reference to variables but not an array in the sense of matrix/arrays. If you really want to use arrays, you'll need to use IML and matrix language. "

 

Also, the solution that you gave is working as expected. Really, thanks a lot.

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
  • 5 replies
  • 834 views
  • 1 like
  • 3 in conversation