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

I need to create a new data set using the Have dataset. The conditons are as follow:

1. Include all observations with Column C = C

2. Select random observations with Column C = T where sum of  TTL_PLST less than or equal to 20. ** Note: The records highlighted in Red make a sum of 20 where Column C = T.**

Dataset Have

Column AColumn BColumn CColumn DTTL_PLSTN
1234567891ACNP11
1234567891ACNP233
1234567891ACNP321
1234567891ACNP42
1234567891ACNP564
1234567891ACNP62
1234567891ACNP797
1234567891ATNP810
1234567891ATNP91
1234567891ATNP103
1234567891ATNP111
1234567891ATNP31
1234567891ATNP132
1234567891ATNP14264
1234567891ATNP151

Want Dataset:

Column AColumn BColumn CColumn DTTL_PLSTN
1234567891ACNP11
1234567891ACNP233
1234567891ACNP321
1234567891ACNP42
1234567891ACNP564
1234567891ACNP62
1234567891ACNP797
1234567891ATNP              8  10
1234567891ATNP              9   1
1234567891ATNP              3   1
1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

how about:

data have ;

input Column_A $9. Column_B $ Column_C $ Column_D $ TTL_PLST N ;

cards;

123456789 1A C  NP 1 1

123456789 1A C  NP 2 33

123456789 1A C  NP 3 21

123456789 1A C  NP 4 2

123456789 1A C  NP 5 64

123456789 1A C  NP 6 2

123456789 1A C  NP 7 97

123456789 1A T  NP 8 10

123456789 1A T  NP 9 1

123456789 1A T  NP 10 3

123456789 1A T  NP 11 1

123456789 1A T  NP 3 1

123456789 1A T  NP 13 2

123456789 1A T  NP 14 264

123456789 1A T  NP 15 1

;

proc sort data=have out=have1(where=(column_c='T'));

  by TTL_PLST;

run;

data have1(drop=total);

  set have1;

  retain total;

   total+TTL_PLST;

  if total <=20;

run;

data want;

   set have (where=(column_c='C'))

     have1;

run;

Linlin

View solution in original post

3 REPLIES 3
Linlin
Lapis Lazuli | Level 10

how about:

data have ;

input Column_A $9. Column_B $ Column_C $ Column_D $ TTL_PLST N ;

cards;

123456789 1A C  NP 1 1

123456789 1A C  NP 2 33

123456789 1A C  NP 3 21

123456789 1A C  NP 4 2

123456789 1A C  NP 5 64

123456789 1A C  NP 6 2

123456789 1A C  NP 7 97

123456789 1A T  NP 8 10

123456789 1A T  NP 9 1

123456789 1A T  NP 10 3

123456789 1A T  NP 11 1

123456789 1A T  NP 3 1

123456789 1A T  NP 13 2

123456789 1A T  NP 14 264

123456789 1A T  NP 15 1

;

proc sort data=have out=have1(where=(column_c='T'));

  by TTL_PLST;

run;

data have1(drop=total);

  set have1;

  retain total;

   total+TTL_PLST;

  if total <=20;

run;

data want;

   set have (where=(column_c='C'))

     have1;

run;

Linlin

Hima
Obsidian | Level 7

Thanks for your quick reply Linlin. Looking at the original data, I totally lost what I was doing. So, I had to post it here.

Ksharp
Super User

Can't understand what you mean about 'Randomly Select observation'

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1259 views
  • 0 likes
  • 3 in conversation