BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Angel_Saenz
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

 

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

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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
Angel_Saenz
Quartz | Level 8

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

 

          

 

SuryaKiran
Meteorite | Level 14

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

Thanks,
Suryakiran
Angel_Saenz
Quartz | Level 8

let me try your solution SuryaKiran

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

SuryaKiran
Meteorite | Level 14

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
Angel_Saenz
Quartz | Level 8
Let me try it SuryaKiran, I accepted other your answer, thanks a lot
SuryaKiran
Meteorite | Level 14

 

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
HB
Barite | Level 11 HB
Barite | Level 11

@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 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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