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

Hi! I would like to store the top 3 values of every id & create a column out of it. And let's say there are only two values present, the columns should be blank


data haveOne;
input id value $;
datalines;
1 ABC
1 EFG
1 XYZ
1 UVW
2 GHJ
2 XYZ
3 ABC;

run;

 

IDValue
1ABC
1EFG
1XYZ
1UVW
2GHJ
2XYZ
3ABC


WantOne

IDValue_1Value_2Value_3
1ABCEFGXYZ
2GHJXYZ.
3ABC..

 

Any kind of help would be appreciated, thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@shubham_d wrote:
For a particular id, I want to store only the first 3 entries

Got it.

 

proc transpose data=haveone out=want(keep=id value_1-value_3) prefix=value_;
by id;
var value;
run;

 

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

What do you mean by “top 3 values”, if the values are character strings?

--
Paige Miller
shubham_d
Fluorite | Level 6
Hey! So these are already sorted values & I need to create a column of only top 3 values
PaigeMiller
Diamond | Level 26

So when I ask "what do you mean by top 3 values", you cannot answer with you want the "top 3 values". That doesn't explain anything.

--
Paige Miller
shubham_d
Fluorite | Level 6
For a particular id, I want to store only the first 3 entries, does that make sense ? And if 3 entries are not present for a particular id, I still want the columns to be created but they will have blank values
PaigeMiller
Diamond | Level 26

@shubham_d wrote:
For a particular id, I want to store only the first 3 entries

Got it.

 

proc transpose data=haveone out=want(keep=id value_1-value_3) prefix=value_;
by id;
var value;
run;

 

--
Paige Miller
shubham_d
Fluorite | Level 6
Hey! Thanks, this helped. Appreciate your patience in understanding the question. Am new to SAS & aiming to be more efficient next time
PeterClemmensen
Tourmaline | Level 20

ID = 3 is not in your data?

shubham_d
Fluorite | Level 6
Sorry missed out the 3rd id in datalines. Updated it now

ID 3 has only one entry & hence when the columns are created the value_1 will have the value & value_2 & value_3 columns will stay blank. I hope that helps.
Tom
Super User Tom
Super User

You can do it using an ARRAY.

First let's create your dataset.  Remember to place the semicolon that ends the data step with in-line data on its own line. Otherwise anything on the line with semicolon is ignored. There is no need to add an extra RUN; statement after the end of the data step.

data have;
  input id value $;
datalines;
1 ABC
1 EFG
1 XYZ
1 UVW
2 GHJ
2 XYZ
3 ABC
;

So to know where in the array to place a value you need to count how many observation you have found.  Here is one way using a DO loop around the SET statement.  This helps as then there is no need to RETAIN the values since all of them for a given ID group are done in one iteration of the data step.


data want;
  do rows=1 by 1 until(last.id);
    set have;
    by id;
    array out $8 value_1-value_3;
    if rows in (1:3) then out[rows]=value;
  end;
  drop value;
run;

Result:

Tom_0-1677775718271.png

 

 

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 9 replies
  • 1672 views
  • 0 likes
  • 4 in conversation