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

Hello,

 

I need to create a new variable that contains the answer with the highest frequency across variables (SAS version 9.4). For example:

id  var

100 2

100 2

100 2

100 4

100 5

101 3

101 3

101 1

101 2

101 3

102 4

102 4

102 5

102 2

102 1

 

I want to add a new variable, so that:

id newvar

100 2

101 3

102 4

 

It seems like there should be a relatively simple answer, but can't seem to figure out. I can transpose to wide if that is better. Any advice will be great, thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Andygray
Quartz | Level 8

@at16 You could mark one of the two solutions that you intend to use as accepted and close the thread.

View solution in original post

6 REPLIES 6
pau13rown
Lapis Lazuli | Level 10

ods output onewayfreqs=test;

proc freq data=;

tables var;

by id;

run;

 

proc sort data=test;

by id descending frequency;

run;

 

proc sort data=test nodupkey;

by id;

run;

 

?

novinosrin
Tourmaline | Level 20
data have;
input id  var;
cards;
100 2
100 2
100 2
100 4
100 5
101 3
101 3
101 1
101 2
101 3
102 4
102 4
102 5
102 2
102 1
;

proc freq data=have order=freq noprint;
by id;
tables var/out=temp(drop=percent);
run;

data want;
set temp;
by id;
if first.id;
run;

Andygray
Quartz | Level 8

@at16 I meant mark the one of the above  contributors who gave you the solution. lol

Andygray
Quartz | Level 8

@at16 You could mark one of the two solutions that you intend to use as accepted and close the thread.

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 959 views
  • 2 likes
  • 4 in conversation