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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.