I have created code till end which create the dataset two if more than 10 observations for row, if not then will create single dataset, my question is how to find and combine datasets which are small as 1 limiting to 10 rows.
data alert;
input alert_id $2. message;
datalines;
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A1 1
A2 1
A2 1
A2 1
A2 1
A2 1
A2 1
A2 1
A2 1
A3 1
A3 1
A3 1
A3 1
A3 1
A3 1
A4 1
A4 1
A4 1
A5 1
A5 1
A5 1
;
proc sql ;
select count(distinct alert_id ) into :n from alert;
select distinct alert_id into :ant1 - :ant1%left(&n) from alert;
quit;
%macro splitd;
%do i =1 %to &n;
%put &&ant&i.;
data &&ant&i.;
set alert;
where alert_id=resolve("&&ant&i.") ;
run;
proc sql;
create table count&i as
select distinct alert_id ,count(* ) as totaobs from work.&&ant&i.
group by alert_id;
select totaobs into :nb&i from count&i;
quit;
%put &&nbl&i;
%let totalds&i=%sysevalf(%left(&&nb&i.)/10,ceil);
%put &&totalds&i.;
%if &&nb&i. >=10 %then %do;
DATA
%DO J=1 %TO &&totalds&i.;
WANT_&i.&j.
%END;;
SET work.&&ant&i.;
%DO J=1 %TO &&totalds&i.;
%IF &J. > 1 %THEN %DO; ELSE %END; IF _N_ LE 10* &J. THEN OUTPUT WANT_&i.&j.;
%END;
RUN;
%end;
%END;;
%mend;
%splitd;
Please try the below code, the count variable carries the number of observation within alert_id if observations are <=10 then all those alert_id records will be outputted to a new dataset want.
data want(where=(count<=10));
do until(last.alert_id);
set alert;
by alert_id;
retain count;
if first.alert_id then count=1;
else count+1;
end;
do until(last.alert_id);
set alert;
by alert_id;
output;
end;
run;
Hi Jag,
Thank you for reply . Here my need to create dataset with limit of 10 and alert_id should not fall under .
for example A1 have 16 , 10 can be created in 1 file and 6 can be created in 1 file.
for A2 there are 8 observations 1 dataset should create.
A3 and A4 has 9 sothat can be created 1 dataset togather .
Can Any one of you reply on this urgent please a bit
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.