BookmarkSubscribeRSS Feed
avama
Calcite | Level 5

Hello. I have the following dataset 

 

IDvalueseq_id
1991
1952
1453
1664
15005
1556
2991
21002
2993
2554
2455
31011
32022
3443
3554

What I would like to do is that

1. If a certain ID group has ALL the values <=100 then I would like to create a separate dataset for that and call it "BLUE" or even just "output column"

2. If a certain ID group has ANY ONE value that is >400 then I would like to create a seperate dataset for that and call it "STOP"

3. If they don't belong in that group then extract only that dataset into "PENDING"

IDvalueseq_idOutput
1991 
1952 
1453 
1664 
15005STOP
1556 
2991BLUE
21002 
2993 
2554 
2455 
31011Pending
32022 
3443 
3554 
2 REPLIES 2
FredrikE
Rhodochrosite | Level 12

How about this?

 

data test;

length ID value seq_id 8;

input ID value seq_id;

datalines;

1 99 1

1 95 2

1 45 3

1 66 4

1 500 5

1 55 6

2 99 1

2 100 2

2 99 3

2 55 4

2 45 5

3 101 1

3 202 2

3 44 3

3 55 4

;

run;

proc sql;

create table test2 as

select id, value, seq_id, max(value) as max

from test

group by id

;

quit;

data blue stop pending;

set test2;

if max > 400 then output stop;

else if max <= 100 then output blue;

else output pending;

run;

Patrick
Opal | Level 21

@avama

Instead of creating multiple data sets, you could also create a grouping variable. 

data have;
  length ID value seq_id 8;
  input ID value seq_id;
  datalines;
1 99 1
1 95 2
1 45 3
1 66 4
1 500 5
1 55 6
2 99 1
2 100 2
2 99 3
2 55 4
2 45 5
3 101 1
3 202 2
3 44 3
3 55 4
;
run;

proc sql;
  create table want as
    select 
      h.*,
      case
        when max(h.value)>400  then 'Stop'
        when max(h.value)<=100 then 'BLUE'
        else 'PENDING'
        end as group length=10
    from have as h
    group by h.id
    order by h.id, h.seq_id
  ;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 773 views
  • 0 likes
  • 3 in conversation