Frequency for multiple variables

Solved
Frequent Contributor
Posts: 94

Frequency for multiple variables

I have a dataset in the following form, with the input variables Var 1 to Var 3 and required output variable:

Var1     Var2     Var3      OutputVar

a          1          p              1

a          1          q              2

a          2          r               1

b          1         m              1

b          2         n               1

b          2         o               2

For each Var1, the OutputVar needs to be the cumulative frequency of Var 3 grouped by Var2. I need all the variables in the final output data.

Thanks.

VD

Accepted Solutions
Solution
‎03-18-2014 12:36 PM
Posts: 1,147

Re: Frequency for multiple variables

data have;

input Var1\$     Var2     Var3\$;

datalines;

a          1          p

a          1          q

a          2          r

b          1         m

b          2         n

b          2         o

;

run;

proc sort data=have;

by var1 var2;

run;

data want;

set have;

by var1 var2;

retain outputvar;

if first.var2 then outputvar=1;

else outputvar+1;

run;

Thanks,

Jag

Thanks,
Jag

All Replies
Solution
‎03-18-2014 12:36 PM
Posts: 1,147

Re: Frequency for multiple variables

data have;

input Var1\$     Var2     Var3\$;

datalines;

a          1          p

a          1          q

a          2          r

b          1         m

b          2         n

b          2         o

;

run;

proc sort data=have;

by var1 var2;

run;

data want;

set have;

by var1 var2;

retain outputvar;

if first.var2 then outputvar=1;

else outputvar+1;

run;

Thanks,

Jag

Thanks,
Jag
Frequent Contributor
Posts: 94

Re: Frequency for multiple variables

Thank you, Jag!

It works well.

VD

🔒 This topic is solved and locked.