Help using Base SAS procedures

How to use .first variable in SAS data step

Reply
New Contributor
Posts: 3

How to use .first variable in SAS data step

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.
Super Contributor
Posts: 359

Re: How to use .first variable in SAS data step

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.
New Contributor
Posts: 3

Re: How to use .first variable in SAS data step

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
SAS Employee
Posts: 160

Re: How to use .first variable in SAS data step

try this simple solution

proc sort data=FirstVisit;
by UserID DATE RecordNo;
run;
proc sort data=FirstVisit NODUPKEY;
by UserID;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: How to use .first variable in SAS data step

Best to also code EQUALS with NODUPKEY - not assumed a default behavior with all sort packages.

Scott Barry
SBBWorks, Inc.
PROC Star
Posts: 1,561

Re: How to use .first variable in SAS data step

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;
SAS Employee
Posts: 160

Re: How to use .first variable in SAS data step

Well then this must be even more clear code ;-)

data FirstVisit;
set FirstVisit ;
by UserID;
if first.UserID then output FirstVisit;
else delete;
run;
New Contributor
Posts: 3

Re: How to use .first variable in SAS data step

I want to thank all of you for replying to my post and providing suggestions - much appreciated.
PROC Star
Posts: 1,561

Re: How to use .first variable in SAS data step

My code (as well as others') should work, unless the issue was misunderstood.
Pls explain what is not right.
Ask a Question
Discussion stats
  • 8 replies
  • 132 views
  • 0 likes
  • 5 in conversation