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

Is there a way to get a frequency between 2 variables, where order doesn't matter?

 

I have a dataset with 2 ratings. Is there an easy way to get a frequency, that combines when R1/R2 is equivalent to R2/R1? This is a very large dataset -- I'm hoping to avoid typing out "IF R1=1 and R2=2 OR R1=2 and R2=1 THEN ID=1", then doing a proc freq statement.

 

For example:

 

 Rating1   Rating2    

    1              2   

    2              1 

    1              3

    1              4

    4              1

 

would be a count of:

2

1

2

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

That's small....

 

Data temp;

set have;

first = min(rating1, rating2);

second = max(rating1, rating2);

Run;

 

proc freq data=temp noprint;

table first*second/out=want;

run;

 

View solution in original post

9 REPLIES 9
ballardw
Super User

How many values of Rating1 and Rating2 are there? Are one or more of the values missing for either of those variables? If missing how would the "combination" of the two be treated?

I would be tempted to create a variable combining Rating1 and 2 then custom informat to turn 12 or 21 into a single category. Then I do a count on the formatted value.

NR13
Fluorite | Level 6

it's 0-16 for both ratings - no missing values for either.

NR13
Fluorite | Level 6

It varies - usually around one thousand

Reeza
Super User

That's small....

 

Data temp;

set have;

first = min(rating1, rating2);

second = max(rating1, rating2);

Run;

 

proc freq data=temp noprint;

table first*second/out=want;

run;

 

ballardw
Super User

And to the OP if you ever need to extend this then the function SMALLEST would come into play.

 

first      = smallest(1,var1,var2,var3,var4);

second = smallest(2,var1,var2,var3,var4);

third      = smallest(3,var1,var2,var3,var4);

fourth    = smallest(4,var1,var2,var3,var4);

 

though it would likely  best to declare a couple of arrays for this.

Astounding
PROC Star

An easy way:

 

data want;

set have;

lower_rating = min(Rating1, Rating2);

high_rating = max(Rating1, Rating2);

run;

 

proc freq data=want;

tables lower_rating * higher_rating / list;

run;

 

If you have missing values, however, this treats the missing rating as if it were the same as the rating that is not missing.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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