DATA Step, Macro, Functions and more

variable consecutively counts account_number

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

variable consecutively counts account_number

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


Accepted Solutions
Solution
‎02-22-2018 05:51 PM
PROC Star
Posts: 635

Re: variable consecutively counts account_number

Posted in reply to Angel_Saenz

 

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;

Thanks,
Suryakiran

View solution in original post


All Replies
Respected Advisor
Posts: 3,294

Re: variable consecutively counts account_number

Posted in reply to Angel_Saenz

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. 

--
Paige Miller
Contributor
Posts: 47

Re: variable consecutively counts account_number

Posted in reply to PaigeMiller

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

 

          

 

PROC Star
Posts: 635

Re: variable consecutively counts account_number

Posted in reply to Angel_Saenz

@Angel_Saenz didn't my solution answer your question. Why do you look for PROC SQL also id DATA Step can solve?

Thanks,
Suryakiran
Contributor
Posts: 47

Re: variable consecutively counts account_number

Posted in reply to SuryaKiran

let me try your solution SuryaKiran

I look a PROC SQL too because I have other plataform and in there only works SQL

PROC Star
Posts: 635

Re: variable consecutively counts account_number

Posted in reply to Angel_Saenz

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;
Thanks,
Suryakiran
Contributor
Posts: 47

Re: variable consecutively counts account_number

Posted in reply to SuryaKiran
Let me try it SuryaKiran, I accepted other your answer, thanks a lot
Solution
‎02-22-2018 05:51 PM
PROC Star
Posts: 635

Re: variable consecutively counts account_number

Posted in reply to Angel_Saenz

 

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;

Thanks,
Suryakiran
Super Contributor
Super Contributor
Posts: 269

Re: variable consecutively counts account_number

[ Edited ]
Posted in reply to SuryaKiran

@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 

 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 151 views
  • 3 likes
  • 4 in conversation