How to extract just the first record in SAS

Reply
Occasional Contributor
Posts: 5

How to extract just the first record in SAS

I have several records per customer and trying to figure out the time between purchases. I already created LAG and DIF variables. I have several records for LAG and DIF for each customer. How do I extract just that first DIF number?

thanks!

Super Contributor
Posts: 644

Re: How to extract just the first record in SAS

LAG() and DIF() should be set to null for the first record per customer.  There is no prior record to difference against.

     PrevBalance = Lag (Balance) ;

     ChngBalance  = Dif (Balance) ;

     If first.customer

          do ;

          Call Missing (PrevBalance) ;

          Call Missing (ChngBalance) ;

          end ;

BTW For documented reasons the following will not work

     If    not first.customer then PrevBalance = Lag (Balance) ;

Richard

Occasional Contributor
Posts: 5

Re: How to extract just the first record in SAS

I used that exact  syntax to extract LAG and DIF. I need to pull out records with the first nont blank DIF value for each customer ID. I want to see when the person orders again, what the amount of time elapses between the first order (first record) and the second record (this is where I will have the first real value for DIF)

Super User
Super User
Posts: 7,413

Re: How to extract just the first record in SAS

Perhaps providing some have and want test data to illustrate would be a good idea.

Super Contributor
Posts: 644

Re: How to extract just the first record in SAS

Add to your code (only outputs the 2nd instance of a customer's records)

    Retain     instance ;

     If first.customer

          do ;

          Instance = 1 ;

          end ;

     instance + 1 ;

     If instance = 2 then output ;


Richard

Super Contributor
Posts: 275

Re: How to extract just the first record in SAS


data want;
  set have;
  by customer_id;
  if first.customer_id then count=0;
  count+1;
  if count=2 then output;
  run;

Ask a Question
Discussion stats
  • 5 replies
  • 227 views
  • 0 likes
  • 4 in conversation