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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.