BookmarkSubscribeRSS Feed
Adi27
Calcite | Level 5

/* Sort the Products dataset by review_count in descending order */
proc sort data=Qed.Products;
by descending review_count;
run;

/* Print the top-most record (most reviewed product) */
data MostReviewedProduct;
set Qed.Products;
if _N_ = 1;
run;

/* Print the most reviewed product */
PROC PRINT DATA=MostReviewedProduct;
TITLE "Most Reviewed Product";
RUN;


/* Sort the Products dataset by review_count in ascending order */
proc sort data=Qed.Products;
by review_count;
run;

/* Print the top-most record (least reviewed product) */
data LeastReviewedProduct;
set Qed.Products;
if _N_ = 1;
run;

/* Print the least reviewed product */
PROC PRINT DATA=LeastReviewedProduct;
TITLE "Least Reviewed Product";
RUN;

/* Compare the most reviewed and least reviewed products */
data ProductComparison;

merge MostReviewedProduct (in=a) LeastReviewedProduct (in=b);
by _all_;

if a and b then do;
if review_count_1 < review_count_2 then do;
message = "Most reviewed product has fewer reviews than the least reviewed product.";
end;
else if review_count_1 > review_count_2 then do;
message = "Most reviewed product has more reviews than the least reviewed product.";
end;
else do;
message = "Most reviewed product has the same number of reviews as the least reviewed product.";
end;
end;
run;
/* Print the comparison result */

PROC PRINT DATA=ProductComparison;
var review_count_1 review_count_2 message;
title "Comparison Between Most Reviewed and Least Reviewed Products";
RUN;

 

HI THIS IS MY CODE AND THE COMPARISON TABLE IS EMPTY

3 REPLIES 3
PaigeMiller
Diamond | Level 26

We would need to see the entire LOG (copied as text and pasted into the window that appears when you click on the </> icon) and also we need to see a portion of your data. Please provide the data as WORKING data step code (Examples and instructions)

--
Paige Miller
Reeza
Super User
data ProductComparison;

merge MostReviewedProduct (in=a) LeastReviewedProduct (in=b);
by _all_;

if a and b then do;

When you merge two data sets, they have the same variable names. SAS will overwrite the variables so you don't have two counts you only have one.

Run just this portion to see what you get before you start doing your analysis. 

 

You need to fix the merge before you do the remainder of the process, primarily by renaming variables of interest or using an append (SET) and reference variables using lag instead.

 

 

ballardw
Super User

When you ask "why" we need either the data files used, which may have problems with sharing, and the log of the step you question. So go to the log where you get the empty table, copy the CODE and all notes or messages from that step. Then on the forum open a text box using the </> icon above the message window and paste the text.

 

Most likely cause from the code I see is that since you use BY _ALL_ in the Merge then one or more variables does not have the same value in both sets. In fact since you sorted and sub-setted the data on Review_count variable values I would be extremely surprised if the "most reviewed" and "least reviewed" have the same number of reviews. So they would not merge onto the same observation.

Pick actual identifying variables for a BY. Do not include variables like "counts" unless you expect most of them to match. And if you have two different variables for the count, implied but not shown, then don't expect them to match very often.

 

Something else,  you may want to rename the Review_count variable from one or both of the sets so that you have two variables to actually compare. I do not see where Review_count_1 or Review_count_2 are assigned/created from I have to assume from Review_count in Qed.Products.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 981 views
  • 0 likes
  • 4 in conversation