DATA Step, Macro, Functions and more

Increasing counter values if not first value

Reply
PROC Star
Posts: 634

Increasing counter values if not first value

I've a dataset as follows and I've to increase to counter value if it is not first error_id and rept_dt.

 

Rept_dt error_id
24/07/2018 500
24/07/2018 500
24/07/2018 300
23/07/2018 200
23/07/2018 200
23/07/2018 150
22/07/2018 100
22/07/2018 120
21/07/2018 100

 

Desired Output:

 

Rept_dt error_id C
24/07/2018 500 1
24/07/2018 500 1
24/07/2018 300 2
23/07/2018 200 1
23/07/2018 200 1
23/07/2018 150 2
22/07/2018 100 1
22/07/2018 120 2
21/07/2018 100 1

 

I tried the code as follows but it is not working. Appreciate if someone of you guide me to resolve the problem.

 

data test;
set tmp3;
by reporting_date perc;
retain perc old_perc;
if first.perc and first.reporting_date then do;
c=1;
old_perc=0;
end;
else do;
if perc < old_perc then c=c+1;
/*else c;*/
end; 
run;
Super User
Super User
Posts: 9,866

Re: Increasing counter values if not first value

data want;
  set have;
  retain c;
  by rept_dt error_id notsorted;
  c=ifn(first.error_id,1,c+1);
run;

Not tested as no test data in the form of a datastep, but that should work.

Super User
Posts: 2,075

Re: Increasing counter values if not first value

data have;
input Rept_dt :ddmmyy10.	error_id;
format Rept_dt ddmmyy10.;
cards;
24/07/2018	500
24/07/2018	500
24/07/2018	300
23/07/2018	200
23/07/2018	200
23/07/2018	150
22/07/2018	100
22/07/2018	120
21/07/2018	100
;

data want;
set have;
by Rept_dt error_id notsorted;
if first.rept_dt then c=1;
else if first.error_id then c+1;
run;
Super User
Posts: 24,027

Re: Increasing counter values if not first value

Your code references variables not shown in the data set, is that intentional or do they not align for a specific reason?

 


@Babloo wrote:

I've a dataset as follows and I've to increase to counter value if it is not first error_id and rept_dt.

 

Rept_dt error_id
24/07/2018 500
24/07/2018 500
24/07/2018 300
23/07/2018 200
23/07/2018 200
23/07/2018 150
22/07/2018 100
22/07/2018 120
21/07/2018 100

 

Desired Output:

 

Rept_dt error_id C
24/07/2018 500 1
24/07/2018 500 1
24/07/2018 300 2
23/07/2018 200 1
23/07/2018 200 1
23/07/2018 150 2
22/07/2018 100 1
22/07/2018 120 2
21/07/2018 100 1

 

I tried the code as follows but it is not working. Appreciate if someone of you guide me to resolve the problem.

 

data test;
set tmp3;
by reporting_date perc;
retain perc old_perc;
if first.perc and first.reporting_date then do;
c=1;
old_perc=0;
end;
else do;
if perc < old_perc then c=c+1;
/*else c;*/
end; 
run;

 

Super User
Posts: 10,860

Re: Increasing counter values if not first value

data have;
input Rept_dt :ddmmyy10.	error_id;
format Rept_dt ddmmyy10.;
cards;
24/07/2018	500
24/07/2018	500
24/07/2018	300
23/07/2018	200
23/07/2018	200
23/07/2018	150
22/07/2018	100
22/07/2018	120
21/07/2018	100
;

data want;
set have;
by Rept_dt error_id notsorted;
if first.rept_dt then c=0;
c+first.error_id ;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 75 views
  • 4 likes
  • 5 in conversation