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;
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.
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;
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;
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;
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.
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.