Help using Base SAS procedures

Please explain to me what does the following data step do "especially the bold big syntax" please email it to me or comment here email(nkogatsetj@gmail.com)

Reply
N/A
Posts: 1

Please explain to me what does the following data step do "especially the bold big syntax" please email it to me or comment here email(nkogatsetj@gmail.com)

DAta Mart.Fica_Lock_up;

set WORK.QUERY_FOR_CUST_E_0002;

by Customer_Key;

IF last.Customer_key then output;

Run;

Trusted Advisor
Posts: 1,630

Re: Please explain to me what does the following data step do "especially the bold big syntax" please email it to me or comment here email(nkogatsetj@gmail.com)

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)

Super Contributor
Posts: 339

Re: Please explain to me what does the following data step do "especially the bold big syntax" please email it to me or comment here email(nkogatsetj@gmail.com)

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_keySmiley Wink, 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

Ask a Question
Discussion stats
  • 2 replies
  • 154 views
  • 0 likes
  • 3 in conversation