BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
West26
Obsidian | Level 7

Hello All,

I need small help with grouping of data. Have two variables (id,desc), with below values

id   desc
01   abc
01   def
01   ghi
01   xyz
02  def
02  ghi
02  xyz
03  lkm
03  ghi
03  opq
03  rst
04  abc
04  opq
04  xyz

I want group id's having desctiptoin only 'def', means

01 def
02 def

in one dataset

and others in seperate data set.

01 abc
01 ghi
01 xyz
02 ghi
02 xyz
03 lkm
03 ghi
03 opq
03 rst
04 abc
04 opq
04 xyz

 

Would like to know how to do this with both Proc sql and datastep(first. and last.)

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

I don't use either "first." or "last." but how about this simple way of writing?

 

data have;
  length id $2 desc $3;
  input id $ desc $;
cards;
01  abc
01  def
01  ghi
01  xyz
02  def
02  ghi
02  xyz
03  lkm
03  ghi
03  opq
03  rst
04  abc
04  opq
04  xyz
;
run;

data one two;
  set have;
  if desc='def' then output one;
  else output two;
run;

proc sql;
  create table one as
  select * from have
  where desc='def';

  create table two as
  select * from have
  where desc^='def';

quit;

View solution in original post

3 REPLIES 3
japelin
Rhodochrosite | Level 12

I don't use either "first." or "last." but how about this simple way of writing?

 

data have;
  length id $2 desc $3;
  input id $ desc $;
cards;
01  abc
01  def
01  ghi
01  xyz
02  def
02  ghi
02  xyz
03  lkm
03  ghi
03  opq
03  rst
04  abc
04  opq
04  xyz
;
run;

data one two;
  set have;
  if desc='def' then output one;
  else output two;
run;

proc sql;
  create table one as
  select * from have
  where desc='def';

  create table two as
  select * from have
  where desc^='def';

quit;
West26
Obsidian | Level 7

Thank you, but I need to reframe my question.

 

DATASET TWO, has to have only below data (exclude those id's from dataset one, having description 'def')

 

DATA SET TWO should be like this
02 ghi
02 xyz
03 lkm
03 ghi
03 opq
03 rst

japelin
Rhodochrosite | Level 12

If you say "exclude those id's from dataset one, having description 'def'", isn't it the following data?

03 lkm
03 ghi
03 opq
03 rst
04 abc
04 opq
04 xyz

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1146 views
  • 0 likes
  • 2 in conversation