user2649353
user2649353

Reputation: 377

How to expand dataset based on categories in SAS? (For loop?)

First off, apologies for the vague title, but I don't really know how to phrase what I'm asking. I have a SAS dataset that gives me unit ID, job ID, and the number of employees in that unit-job combination for each of several wage bands (ie, for unit 1, job A, there are 3 employees in band A, 2 employees in band B, etc.). I want to blow this up so that I have a single employee in each row in order to measure dispersion, variance, mean, median, etc. Below is an example of what I have and what I want. Any help would be greatly appreciated. Thanks!

What I have:

data have;
length id1 id2 $20;
infile datalines dlm=' ';
input id1 $ id2 $ a  b  c ;
datalines;
123 A1 2 1 0
123 A2 0 1 1
123 A3 1 0 0
456 A1 0 3 0
456 A2 1 1 1
456 A3 1 2 1
;

What I want to turn it into:

                                  ID1    IDS    Category

                                  123    A1       A
                                  123    A1       A
                                  123    A1       B
                                  123    A2       B
                                  123    A2       C
                                  123    A3       A
                                  456    A1       B
                                  456    A1       B
                                  456    A1       B
                                  456    A2       A
                                  456    A2       B
                                  456    A2       C
                                  456    A3       A
                                  456    A3       B
                                  456    A3       B
                                  456    A3       C

Upvotes: 0

Views: 83

Answers (1)

stat
stat

Reputation: 669

I hope that you will find a better solution, I can't figure out which is really the result that you want to get. Anyway, the requested dataset could be derived with a pair of nested do loops:

data prova;
length id1 id2 a b c $20;
infile datalines dlm=' ';
input id1 $ id2 $ a $ b $ c $;
datalines;
123 A1 2 1 0
123 A2 0 1 1
123 A3 1 0 0
456 A1 0 3 0
456 A2 1 1 1
456 A3 1 2 1
;

data prova01;
length category $20;
set prova;

array ncats[*] a b c;
array chcats[*] $ ac bc cc ('a' 'b' 'c');

do i=1 to dim(ncats);
do j=1 to ncats[i];
category=chcats[i];
output;
end;
end;

drop i j ac bc cc a b c;
run;

Upvotes: 1

Related Questions