RECENGINE Procedure

Example 28.2 Running the KNN Method of the Recommender Engine Model on Explicit Feedback

This example shows how to use PROC RECENGINE to train a k-nearest-neighbors model on an explicit feedback data set, the MovieLens data set. The MovieLens data set was developed by the GroupLens project at the University of Minnesota and is available at http://grouplens.org/datasets/movielens . This example uses the MovieLens 1M version. You can download the compressed archive file from the website at http://files.grouplens.org/datasets/movielens/ml-1m.zip and use any third-party unzip tool to extract all the files in the archive to the destination directory of your choice.[7] The file that contains the ratings is ratings.dat. The data set consists of movie ratings that are provided by a collection of users. For each observation, a user (indicated by the user ID column) has provided a rating from 1 to 5, as indicated by the rating column for a movie (indicated by the item ID column). The data set includes 1,000,209 rating events (user feedback). The following DATA step loads the data table from the directory into your session:

   data mylib.movieRatings;
      infile 'ratings.dat' DLMSTR = '::';
      input userid itemid rating timestamp;
   run;

The following statements use the KNN method that is implemented in PROC RECENGINE to train a k-nearest-neighbors model on the mylib.movieRatings data:


proc recengine data=mylib.movieRatings method =KNN outModel=mylib.nearestNeighbors;
     input     rating / level=interval;
     input     userid itemid / level=nominal;
     userid    userid;
     itemid    itemid;
     knn       k=5 distanceMetric=cosine outSimilarity=mylib.nearestNeighborValues;
     target    rating;
     savestate rstore=mylib.astoreModel;
run;


When the training is completed, PROC RECENGINE produces three output data tables: nearestNeighbors, nearestNeighborValues, and astoreModel. The nearestNeighbors table contains the nearest neighbors for each item; the nearestNeighborValues table contains the similarity scores for each item’s nearest neighbors; and the astoreModel table contains the nearest neighbors, similarity scores, and training data.

The following statements print the nearest neighbors of the first 10 items in the nearestNeighbors table. The results are shown in Output 28.2.1.

   proc print data=mylib.nearestNeighbors(obs=10);
   run;

Output 28.2.1: Nearest Neighbors of the First 10 Items

ObsVariableLevelNeighbor1Neighbor2Neighbor3Neighbor4Neighbor5
1itemid13114126558823551270
2itemid1017221370733648349
3itemid10083215894542251404
4itemid10001470306529558471351
5itemid100210682118503228633
6itemid1003159828022611518
7itemid10041626204168714971382
8itemid10052042208237410212953
9itemid100610031672805280350
10itemid100720162095101010111009


The following statements print the nearest-neighbor similarity scores of the first 10 items in the nearestNeighborValues table. The results are shown in Output 28.2.2.


   proc print data=mylib.nearestNeighborValues(obs=10);
   run;

Output 28.2.2: Nearest-Neighbor Similarity Scores of the First 10 Items

ObsVariableLevelSimilarity1Similarity2Similarity3Similarity4Similarity5
1itemid10.633100.610830.605850.579380.57013
2itemid100.671990.573200.566660.560450.55527
3itemid1000.305630.298160.297130.295490.28901
4itemid10000.272800.272800.232640.227960.21858
5itemid10020.372270.309840.296910.288680.27031
6itemid10030.453250.386970.364520.342440.33584
7itemid10040.551410.439950.390070.381280.36717
8itemid10050.672950.441300.396290.385430.38217
9itemid10060.269970.267030.253050.248940.24633
10itemid10070.479010.418690.399980.372020.36637


The following two code blocks show how to find the top five items for a sample user, whose user ID is 6040, by using the built-in analytic store scoring function. First, you create the scoring data table that includes the user ID of interest:

      data mylib.user6040;
      input   userid;
      datalines;
      6040
      ;
      run;

Next, you use the ASTORE procedure to score the binary-format output data table astoreModel for this user. In this scoring example, the following parameters are specified in SETOPTION statements:

  • REC_TOP_N is set to 5, resulting in five recommendations for user 6040.

  • AGGREGATION_METHOD is set to 0, indicating that shared similarity scores are to be summed.

  • REC_ITEM_FILTER is set to 1, indicating that the user’s recommendations cannot include items that the user has interacted with during model training. This is the default behavior during scoring.


   proc astore;
      setoption REC_TOP_N            5;
      setoption AGGREGATION_METHOD   0;
      setoption REC_ITEM_FILTER      1;
      score data=mylib.user6040 rstore=mylib.astoreModel out=mylib.rankedItemsKnn;
      run;
   quit;

The following statements print the five most recommended items for user 6040. The results are shown in Output 28.2.3.

   proc print data=mylib.rankedItemsKnn;
   run;

Output 28.2.3: Top Five Recommended Items for User 6040

Obsuserid_RECOMMENDED_itemid_SCORE_RANK
16040236051
2604094252
3604092653
4604091554
5604091155




[7] Disclaimer: SAS may reference other websites or content or resources for use at Customer’s sole discretion. SAS has no control over any websites or resources that are provided by companies or persons other than SAS. Customer acknowledges and agrees that SAS is not responsible for the availability or use of any such external sites or resources, and does not endorse any advertising, products, or other materials on or available from such websites or resources. Customer acknowledges and agrees that SAS is not liable for any loss or damage that may be incurred by Customer or its end users as a result of the availability or use of those external sites or resources, or as a result of any reliance placed by Customer or its end users on the completeness, accuracy, or existence of any advertising, products, or other materials on, or available from, such websites or resources.

Last updated: September 23, 2026