Hi, how can I generate a variable that consecutively counts the variable id? If I have a dataset where account_number is not unique.
Example I want:
varCONS account_number
1 1
2 1
1 2
2 2
3 2
1 3
1 4
2 4
3 4
4 4
If you can help me with two metods, data step and proc sql could be great, thanks
DATA HAVE;
infile datalines;
input id;
datalines;
1
1
2
1
2
3
3
4
1
;
run;
proc sort data=have ;
by id;
run;
data want;
set have;
by id;
if first.id then account_num=1;
else account_num+1;
run;
It sure would help if you specified the criteria by which we do the counting, instead of us having to figure out what you want (and possibly get it wrong and waste our time and everyone else's time).
So I'm going to guess that whenever you see the number 1, you increment the account_number. Is that right????
Here's some code to do this.
data want;
set have;
if varcons=1 then account_number+1;
run;
As far as SQL goes, I think it is folly to try to do this in SQL, and so I provide no example.
Thanks for your answer, but no, I dont want increment the account_number, I have a dataset with some account numbers that are not unique, some like this:
account_number
1
1
1
2
3
3
3
3
4
4
4
So I want count this for each account_number:
account_number var_I_WANT
1 1
1 2
1 3
2 1
3 1
3 2
3 3
3 4
4 1
4 2
4 3
@Angel_Saenz didn't my solution answer your question. Why do you look for PROC SQL also id DATA Step can solve?
let me try your solution SuryaKiran
I look a PROC SQL too because I have other plataform and in there only works SQL
Try this:
DATA HAVE;
infile datalines;
input id;
/*row=monotonic();*/
datalines;
1
1
2
1
2
3
3
4
1
;
run;
proc sql;
create table want as
select a.ID,count(*) as count
from (select ID,MONOTONIC() AS Row
from have) a
left join (select ID,MONOTONIC() AS Row
from have) b
on a.id=b.id and a.row>=b.row
group by a.id,a.row
;
quit;
DATA HAVE;
infile datalines;
input id;
datalines;
1
1
2
1
2
3
3
4
1
;
run;
proc sort data=have ;
by id;
run;
data want;
set have;
by id;
if first.id then account_num=1;
else account_num+1;
run;
@SuryaKiran 's solution is the way to go.
It can be done in SQL, just not in SQL that is supported inside SAS.
http://sqlfiddle.com/#!18/26c26/6
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: