BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

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;
4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

novinosrin
Tourmaline | Level 20
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;
Reeza
Super User

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;

 

Ksharp
Super User
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;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1582 views
  • 4 likes
  • 5 in conversation