BookmarkSubscribeRSS Feed
SASNovice
Calcite | Level 5
Scenario: Want to pull only the first record of a dataset by user ID (may be duplicates for any given user) by earliest date and record number ID (may be duplicates for any given user). This is what I’ve tried so far and it is not always pulling the first visit.

proc sort data=FirstVisit;
by UserID DATE RecordNo;
run;

data FirstVisit;
set FirstVisit;
/*maybe I should be using a retain statement here???)
by UserID DATE RecordNo;
/*If it was their first offer*/
if first.UserID then VisitType = 'Initial Visit';
else VisitType = 'FollowupVisit';
run;

proc sort data= FirstVisit;
by VisitType UserID;
run;

data Visits;
set Visits;
keep UserID VisitType DATE RecordNo;
if VisitType = ‘Initial Visit';
run;

Thanks for any suggestions re above data steps or alternative code you may be able to provide. Much appreciated.
8 REPLIES 8
Flip
Fluorite | Level 6
I ran your code ( after fixing open ended comments and differing dataset names) and got the first visit record every time.

Are you using SAS Dates?
Perhaps you could post more about your data that would point to the problem.
SASNovice
Calcite | Level 5
I have reviewed all the responses received re .first variable data step. Haven't found a solution yet. In your message, you said that you were sucessful in running code... to get the first record. Would you be able to post this code for me and I'll try it in my program.

Thanks for your follow-up.

P.S. I am using SAS dates
GertNissen
Barite | Level 11
try this simple solution

proc sort data=FirstVisit;
by UserID DATE RecordNo;
run;
proc sort data=FirstVisit NODUPKEY;
by UserID;
run;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Best to also code EQUALS with NODUPKEY - not assumed a default behavior with all sort packages.

Scott Barry
SBBWorks, Inc.
ChrisNZ
Tourmaline | Level 20
Better to make one's logic explicit imho:

proc sort data=FirstVisit;
by UserID DATE RecordNo;
run;
data FirstVisit
set FirstVisit ;
by UserID;
if first.UserID;
run;
GertNissen
Barite | Level 11
Well then this must be even more clear code 😉

data FirstVisit;
set FirstVisit ;
by UserID;
if first.UserID then output FirstVisit;
else delete;
run;
SASNovice
Calcite | Level 5
I want to thank all of you for replying to my post and providing suggestions - much appreciated.
ChrisNZ
Tourmaline | Level 20
My code (as well as others') should work, unless the issue was misunderstood.
Pls explain what is not right.

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!

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
  • 8 replies
  • 801 views
  • 0 likes
  • 5 in conversation