Have:
id | v1 | v2 | v3 | v4 | v5 |
1 | 0 | 0.1 | 1 | 1 | 0.1 |
1 | 0 | 2 | 0 | 0.1 | |
1 | 5 | 2 | 0 | 0.1 | |
1 | 10 | 2 | 0 | 0.1 | |
1 | 15 | 2 | 0 | 0.1 | |
1 | 20 | 2 | 0 | 0.1 | |
1 | 25 | 2 | 0 | 0.1 | |
1 | 30 | 2 | 0 | 0.1 | |
1 | 0 | 0.1 | 1 | 1 | 0.2 |
1 | 0 | 2 | 0 | 0.2 | |
1 | 5 | 2 | 0 | 0.2 | |
1 | 10 | 2 | 0 | 0.2 | |
1 | 15 | 2 | 0 | 0.2 | |
1 | 20 | 2 | 0 | 0.2 | |
1 | 25 | 2 | 0 | 0.2 | |
1 | 30 | 2 | 0 | 0.2 |
want:
id | v1 | v2 | v3 | v4 | v5 | count |
1 | 0 | 0.1 | 1 | 1 | 0.1 | 1 |
1 | 0 | 2 | 0 | 0.1 | 1 | |
1 | 5 | 2 | 0 | 0.1 | 1 | |
1 | 10 | 2 | 0 | 0.1 | 1 | |
1 | 15 | 2 | 0 | 0.1 | 1 | |
1 | 20 | 2 | 0 | 0.1 | 1 | |
1 | 25 | 2 | 0 | 0.1 | 1 | |
1 | 30 | 2 | 0 | 0.1 | 1 | |
1 | 0 | 0.1 | 1 | 1 | 0.2 | 2 |
1 | 0 | 2 | 0 | 0.2 | 2 | |
1 | 5 | 2 | 0 | 0.2 | 2 | |
1 | 10 | 2 | 0 | 0.2 | 2 | |
1 | 15 | 2 | 0 | 0.2 | 2 | |
1 | 20 | 2 | 0 | 0.2 | 2 | |
1 | 25 | 2 | 0 | 0.2 | 2 | |
1 | 30 | 2 | 0 | 0.2 | 2 |
I am trying to get an enumeration variable (count) which indicates number of different v5 groups by ID.
Please help me to derive count variable.
Thanks
proc sort data=have;
by id v5;
run;
/* just to be sure */
data want;
set have;
by id v5;
retain count;
if first.id then count = 0;
if first.v5 then count + 1;
run;
You need clear rules on where to restart/increment your counts. Based on sample data this could work, but it depends on how closely it matxhes your real data.
Data want;
set have;
by id v1;
Retain count 0;
if v1=0 and v2=0.1 then count+1;
run;
Hi,
data want; set have; by id; retain count; if _n_=1 then count=0; if first.id then count=count+1; run;
HI RW9,
this code gives the enumeration for the id but doesnt give the enumeration for the variable 'V5'
Thanks RW9
proc sort data=have;
by id v5;
run;
/* just to be sure */
data want;
set have;
by id v5;
retain count;
if first.id then count = 0;
if first.v5 then count + 1;
run;
Thanks Kurt.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.