BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi guys suppose to have the following table: 

 

_FREQ_ Count_1 Count_2
24213 30240 465
21292 58684 16485
11984 4168 332
24981 130459 33411

 

Is there a way to divide Count_* column by _FREQ_rowise? 

Let say: 30240/2413 then 58684/21292.... for Count_1 and 

                465/24213 then 16485/21292 .... for Count_2 and so on? 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So this is your real issue.

That's what arrays are for:

data want;
set have;
array count {28} count1-count28;
array ratio {28} ratio1-ratio28;
do i = 1 to 28;
  ratio{i} = count{i} / _freq_;
end;
drop i;
run;

But whenever I see such data structures, I suspect that a long layout would be much easier to work with from the beginning, even the PROC SUMMARY code would be simpler.

View solution in original post

5 REPLIES 5
NewUsrStat
Pyrite | Level 9
Thank you for your help! I have 28 columns I would like to avoid write the ratios every time
Kurt_Bremser
Super User

So this is your real issue.

That's what arrays are for:

data want;
set have;
array count {28} count1-count28;
array ratio {28} ratio1-ratio28;
do i = 1 to 28;
  ratio{i} = count{i} / _freq_;
end;
drop i;
run;

But whenever I see such data structures, I suspect that a long layout would be much easier to work with from the beginning, even the PROC SUMMARY code would be simpler.

ballardw
Super User

I believe we have suggested providing data in the form of working data step code so we have actual data to work with and don't have to ask questions about it.

 

If I understand your question I think this is what you want:

data want;
   set have;
   rate1 = count_1 / _freq_;
   rate2 = count_2 / _freq_;
run;

If you have 'many' count or other numerator variables that you want then ARRAY processing can reduce the total amount of code:

data want;
   set have;
   array c (*) count_1 count_2; /*<= list of the variables you have*/
   array rate(2) ;  /* the number in () should match the number of "count" variables*/
   do i=1 to dim(c);
       rate[i] = c[i] / _freq_;
   end;
   drop i;
run;

Which if you had 20 count variables the only changes needed would be 1) the list (note SAS allows several list forms such as Count_1 - Count_20 if the variables are named such to simplify name lists)

and 2) the number of elements in the Rate array.

The array statement will create N variables named <name>1 to <name>N where N is the number in parentheses by default. Or you can explicitly name your variables with a list such as shown for the C array.

The function DIM is a special function that returns the number of defined elements in an array. So can be used in a loop as shown to get each index number and select the desired element from the array.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 476 views
  • 1 like
  • 3 in conversation