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;

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
  • 4 replies
  • 709 views
  • 4 likes
  • 5 in conversation