user1626092
user1626092

Reputation: 499

SAS max value of a varible

I have a probably very simple question. I have a dataset with rows of different time periods. Every year a new time period is added to the dataset. In my dataset I have period 52, 53 and 54. Soon period 55 will be added. In the code below I extract the rows from the latest period (54):

data new;
set old;
if period=54;
run;

Instead of hardcoding 54 (and soon 55) I want SAS to be able to identify the largest time period, so I don't have to correct the number every time a new period is added to the dataset. I have tried this:

data new;
set old;
if period=max(period);
run;

But the max function requires at least two arguments, and that is not what I want. Can anybody tell me which function to use instead?

Thanks a lot.

Kind regards Maria

Upvotes: 0

Views: 276

Answers (3)

data _null_
data _null_

Reputation: 9109

You should consider keeping the last period in a data set so you don't have to calculate it each time you need to access the data. Also create an INDEX on period to optimize retrieval.

25         options msglevel=i;
26         data example(index=(period)) lastperiod(keep=period);
27            set sashelp.class(in=in1) sashelp.class(in=in2) end=eof;
28            period = ifn(in1,54,55);
29            output example;
30            if eof then output lastperiod;
31            run;
32         
33         data _null_;
34            set period;
35            call symputx('LastPeriod',period);
36            run;
37         data period;
38            set example;
39            where period eq &lastperiod;
INFO: Index period selected for WHERE clause optimization.
40            run;

Upvotes: 2

cstotzer
cstotzer

Reputation: 415

Creating a subset of old containing only the newest period can be done easily using PROC SQL:

PROC SQL NOPRINT;
  CREATE TABLE new AS
  SELECT *
    FROM old
   WHERE period = (SELECT MAX(period) FROM old);
QUIT;

Upvotes: 0

kl78
kl78

Reputation: 1666

One option would be to use a macrovariable and a proc sql step to get the maxperiod, e.g.:

proc sql;
select max(period) into: maxp from old;
quit;

data new;
set old;
if period = "&maxp"; 
run;

Upvotes: 1

Related Questions