Solving the LinkedIn Queens Puzzle with PROC OPTMODEL
Recent Library Articles
Recently in the SAS Community Library: SAS' @SubbuPaz and @RobPratt formulate the LinkedIn Queens Puzzle as an integer programming model and solve it using PROC OPTMODEL in SAS Optimization.
I'm having trouble figuring out how to create two new variables that depend on the most recent date. Let's say I have ID, dx (diagnosis), a prescribed med, and a start date.
data have;
input id dx $ med $ startdate :date9.;
format startdate date9.;
datalines;
1 a a 01JAN2020
1 a a 04APR2020
1 a a 21APR2020
1 a a 01JUL2020
2 a a 01FEB2020
2 a b 01JUL2020
2 a a 07JUL2020
3 b b 01JAN2020
3 b a 04APR2020
3 b a 01MAY2020
3 b a 15JUN2020
3 b a 01SEP2020
3 b b 02SEP2020
;
RUN;
For any id, when there is > 60 day gap from the preceding date, given the same dx and med, I want to call this is a "new" prescription. For the same dx and med <60 days from the most recent date I want to call it a "refill".
data want;
input id dx $ med $ startdate :date9. refill $ new $;
format startdate date9.;
datalines;
1 a a 01JAN2020 0 0
1 a a 04APR2020 0 1
1 a a 21APR2020 1 0
1 a a 01JUL2020 0 1
2 a a 01FEB2020 0 0
2 a a 07JUL2020 0 1
2 a b 01JUL2020 0 0
3 b a 04APR2020 0 0
3 b a 01MAY2020 1 0
3 b a 15JUN2020 1 0
3 b a 01SEP2020 0 1
3 b b 01JAN2020 0 0
3 b b 02SEP2020 0 1
;
RUN;
The code I've been trying to use is below.
proc sort data=have out=havesort;
by id dx med startdate;
run;
data want;
set havesort;
by id dx med;
IF FIRST.med THEN DO;
TEMP=startdate;
refill=0;
new=0;
END;
RETAIN TEMP refill new;
IF startdate>TEMP + 60 THEN new+1;
IF TEMP<startdate<=TEMP + 60 then refill+1;
TEMP=startdate;
drop temp;
run;
However, as you can see, this doesn't reset the 60 day counter to the most recent startdate. How can I do that to get what I want?
Let me know if that does not make sense. Thank you always for the help!
... View more
Hello, This is probably an obvious answer, but does Base SAS 9.4 M6 and PC-SAS 9.4 TS Level 1M7 produce different random number generations in PROC MCMC even if the same seed is set? I'm not able to replicate the posterior datasets, but the posterior summaries are close to 2 decimal places at least. Is there a way I can set an older version of SAS in 1M7 in order to replicate? Thank you.
... View more
I have several groups of data with varying number of rows of observations. How do I fill the data so that each group has the same number of rows, say 13, of observations filled with the last row of observation available?
... View more
Hello, I have read some posts on this topic, but nothing is working for me. I have a dataset with a variable called cal_date that is numeric with a date format (date11). In SAS 9.4 Proc Report I am using ODS tagsets and the define statement is as follows: DEFINE cal_date/display 'Calendar Date' format=date11.; I have tried this too - DEFINE cal_date/display 'Calendar Date' format=date11. style(column) = {tagattr="format:dd-mmm-yyyy"}; But in the excel output, I want to be able to use the date filter on this column and I cannot. It just gives me the text filter which is not helpful. Any suggestions? Thanks!
... View more
Hi, I have been working on the Essentials 1 Case Study looking at TSA Claims data. I have reviewed all the documents and am still having troubles with my code. For the total number of date issues, I got 3938 but the document says it should be 4241. For the part with the macro for StateName, every time I run it there is no results/output and the log says: NOTE: No observations were selected from data set TSA.CLAIMS_CLEANED. NOTE: There were 0 observations read from the data set TSA.CLAIMS_CLEANED. WHERE 0 /* an obviously FALSE WHERE clause */ ; I even copied and pasted all of the code from the document and still receive the same number (3938) and same log message from my original code. When the macro is for "C" then there are results that show up, but none for the example with "California" (as per the document code). I noticed in the proc contents that the length of State and StateName was only 1, perhaps that's the problem? I tried changing the length of those in the data steps, but that didn't change anything. Can someone please help me make this work? Below is my code after I set up my library: options validvarname=v7;
proc import datafile="&path/TSAClaims2002_2017.csv"
dbms=csv out=ClaimsImport replace;
guessingrows=max;
run;
/* EXPLORE DATA */
/* Look at first 20 observations */
proc print data=tsa.claimsimport (obs=20);
run;
/* Look at the descriptor portion of the data, sorted bycolumn order. */
proc contents data=tsa.claimsimport varnum;
run;
proc freq data=tsa.claimsimport;
tables Claim_Site Disposition Claim_Type / nocum nopercent;
tables Incident_Date Date_Received / nocum nopercent;
format Incident_Date Date_Received year4.;
run;
/* PREPARE DATA */
/* Remove entirely duplicate rows and create new table */
proc sort data=tsa.claimsimport
out=tsa.Claims_NoDups
nodupkey;
by _all_;
run;
proc sort data=tsa.claims_nodups;
by Incident_Date;
run;
data tsa.Claims_Cleaned;
set tsa.claims_nodups;
/* Clean Claim_Site by replacing missing and - values with Unknown */
if Claim_Site in ('-',"") then Claim_Site = "Unknown";
/*Clean Disposition */
if Disposition in ("-", "") then
Disposition = 'Unknown';
else if Disposition = 'Closed: Canceled' then
Disposition = 'Closed:Canceled';
else if Disposition = 'losed: Contractor Claim' then
Disposition ='Closed:Contractor Claim';
/*Clean Claim_Type*/
if Claim_Type in ("-","") then Claim_Type = "Unknown";
else if Claim_Type = 'Passenger Property Loss/Injury' then Claim_Type = 'Passenger Property Loss';
else if Claim_Type = 'Passenger Property Loss/Injur' then Claim_Type = 'Passenger Property Loss';
else if Claim_Type = 'Property Damage/Personal Injury' then Claim_Type = 'Property Damage';
/* Convert State values to uppercase and all StateName Columns to proper case */
State=upcase(state);
StateName=propcase(StateName);
/* Create columns that identifies rows with issues */
if (Incident_Date > Date_Received or
Incident_Date = . or
Date_Received = . or
year(Incident_Date) < 2002 or
year(Incident_Date) > 2017 or
year(Date_Received) < 2002 or
year(Date_Received) > 2017)
then Date_Issues = "Needs Review";
/* Create permanent labels for columns */
format Incident_Date Date_Received date9. Close_Amount dollar20.2;
label Airport_Code = "Airport Code"
Airport_Name = "Airport Name"
Claim_Number = "Claim Number"
Claim_Site = "Claim Site"
Claim_Type = "Claim Type"
Close_Amount = "Close Amount"
Date_Issues = "Date Issues"
Date_Received = "Date Received"
Incident_Date = "Incident Date"
Item_Category = "Item Category";
/* Drop County and City Columns */
drop County City;
run;
/* ANALYZE */
/* How many date issues are in the overall data? 3938 */
title "Overall Date Issues in the Data";
proc freq data=tsa.Claims_Cleaned;
table Date_Issues / nocum nopercent;
run;
title;
/* How many claims per year of Incident_date are there in the overall data?
Values vary between years. There were 2122 in 2002 and 8340 in 2017 */
/* Include a plot */
ods graphics on;
title "Overall Claims by Year";
proc freq data=tsa.Claims_Cleaned;
table Incident_Date / nocum nopercent plots=freqplot;
format Incident_Date year4.;
where Date_Issues is missing; /* filter where rows DO NOT have any date issues */
run;
/* What are the frequency values for Claim_Type, Claim_Site, and Disposition for */
/* a selected state? NOTE: User should be able to dynamically input a specific state */
%let StateName=California;
proc freq data=tsa.Claims_Cleaned order=freq;
tables Claim_Type Claim_Site Disposition / nocum nopercent;
where StateName="&StateName" and Date_Issues is null; /* Filter for only rows where colunm has missing value */
run;
proc means data=tsa.Claims_Cleaned mean min max sum maxdec=0;
var Close_Amount;
where StateName="&StateName" and Date_Issues is null;
run;
... View more
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Nominations are in, and the SAS Customer Recognition Awards voting is complete! Winners get a full trip to SAS Innovate (May 6-9) in Orlando, FL! See the 60+ inspiring entries from SAS users! Winners will be announced at SAS Innovate!