## Counting variables in a variable list

Solved
Occasional Contributor
Posts: 16

# Counting variables in a variable list

I am running into some issues using a macro code-I want to count/summing the number of variables in a list for an entire dataset. Say for instance my dataset looks like the following:

ID                    Factors

123                   A3, A4, A5

233                   A3

451                   A4, A5, F5

567                   F5

112                   A1

344                  A3, A5

557                  A4

My goal is to create one large variable list (A3, A4, A5, A3, A4, A5, F5, F5, A1, A3, A5, A4) to then count the frequency of each factor (I know it will not total 100% since more than one attribute can be reported per ID) to get this:

Factor            Value

A1                     1

A3                     3

A4                     3

A5                     3

F5                     2

Any help would be greatly appreciated.

Accepted Solutions
Solution
‎04-28-2014 03:15 PM
Respected Advisor
Posts: 3,167

## Re: Counting variables in a variable list

Here is an alternative using Hash object:

data have;

input ID \$3. Factors :\$&20.;

cards;

123                   A3, A4, A5

233                   A3

451                   A4, A5, F5

567                   F5

112                   A1

344                  A3, A5

557                  A4

;

data _null_;

if _n_=1 then do;

dcl hash h(ordered:'a');

h.definekey('Factor');

h.definedata('Factor','Value');

h.definedone();

length factor \$ 8;

call missing (factor,value);

end;

set have end=last;

do i=1 by 1 while (not missing(scan(factors,i)));

factor=scan(factors,i);

rc=h.find();

if rc ne 0 then value=0;

value=value+1;

rc=h.replace();

end;

if last then h.output(dataset:'want');

run;

Haikuo

All Replies
PROC Star
Posts: 8,164

## Re: Counting variables in a variable list

Couldn't you just create a longer dataset and use proc freq?  e.g.,

data have;

informat Factors \$30.;

input ID Factors &;

cards;

123                   A3, A4, A5

233                   A3

451                   A4, A5, F5

567                   F5

112                   A1

344                  A3, A5

557                  A4

;

data need (drop=factors);

set have;

_n_=1;

do while (scan(factors,_n_,',') ne "");

factor=strip(scan(factors,_n_,','));

_n_ + 1;

output;

end;

run;

proc freq data=need;

tables factor;

run;

Solution
‎04-28-2014 03:15 PM
Respected Advisor
Posts: 3,167

## Re: Counting variables in a variable list

Here is an alternative using Hash object:

data have;

input ID \$3. Factors :\$&20.;

cards;

123                   A3, A4, A5

233                   A3

451                   A4, A5, F5

567                   F5

112                   A1

344                  A3, A5

557                  A4

;

data _null_;

if _n_=1 then do;

dcl hash h(ordered:'a');

h.definekey('Factor');

h.definedata('Factor','Value');

h.definedone();

length factor \$ 8;

call missing (factor,value);

end;

set have end=last;

do i=1 by 1 while (not missing(scan(factors,i)));

factor=scan(factors,i);

rc=h.find();

if rc ne 0 then value=0;

value=value+1;

rc=h.replace();

end;

if last then h.output(dataset:'want');

run;

Haikuo

Occasional Contributor
Posts: 16

## Re: Counting variables in a variable list

Thank you so much Hai.kuo

Respected Advisor
Posts: 3,852

## Re: Counting variables in a variable list

You use the terms "variable list" and "factor".   What do you mean?

🔒 This topic is solved and locked.

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

Discussion stats
• 4 replies
• 248 views
• 3 likes
• 4 in conversation