BookmarkSubscribeRSS Feed
basani
Calcite | Level 5

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;

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
basani
Calcite | Level 5
It is creating want single dataset which contains 20 orbs from alert, what
is I required is a2’s 8 observations in 1 dataset and reaming a3 and a4 in
one a5 in 1 dataset each dataset should not be more than 10 obs and alertid
should not fall in another dataset, please help me . Thank you for your
reply
basani
Calcite | Level 5

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 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1024 views
  • 0 likes
  • 2 in conversation