BookmarkSubscribeRSS Feed
tebogo
Calcite | Level 5

DAta Mart.Fica_Lock_up;

set WORK.QUERY_FOR_CUST_E_0002;

by Customer_Key;

IF last.Customer_key then output;

Run;

2 REPLIES 2
PaigeMiller
Diamond | Level 26

It outputs to dataset Mart.Fica_Lock_up the LAST sorted observation of every by group (in this case by-groups of the variable customer_key).

It might be helpful if your title line spoke about the actual problem you have instead of how the problem is identified in the text (example: What does "if last.customer_key" do)

--
Paige Miller
Vince28_Statcan
Quartz | Level 8

First. and Last. are fairly well explained in SAS documentation. You may want to search and read it for yourself. In the mean time, here's my quick explanation:

When you use BY group processing (statement by customer_key;), SAS creates 2 additionnal automatic variables per by variable to help you process your groups.

Assume the following statement:

by var1 var2 var3;

then SAS creates

first.var1 last.var1

first.var2 last.var2

first.var3 last.var3

automatic variables. They are binary (0 or 1) variables that indicate whether the current record is the first (Resp. last) in the current group.

Since FALSE=0 and TRUE=1, the statement IF last.customer_key then output; is equivalent logically to IF last.customer_key=TRUE then output;

That is, the above statement controls your output and only outputs the last record for each distinct customer_key in query_for_cust_e_0002 to mart.fica_lock_up

Imagine the following data set that you read with a BY var1 var2 var3; statement:

rownum var1 var2 var3

1 1 1 1

2 1 2 1

3 1 2 2

4 1 2 3

5 1 3 1

In the Above, first.var1 = 1 only for rownum=1. Last.var1=1 only for rownum=5

first.var2=1 for each rownum in (1, 2, 5) and last.var2=1 for each rownum in (1, 4, 5)

first.var3=1 for each rownum in (1,2,3,4,5) and last.var3=1 for each rownum in (1,2,3,4,5)

Thus, the first and last automatic variables created allow you, for example, to reset counters, reset sums to 0 or products to 1 for your desired by group. They also allow you control over time series where you are only interested in values at the begining and end of a by group, etc.

Hope this helps.

Vince

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 679 views
  • 0 likes
  • 3 in conversation