Solved
Contributor
Posts: 20

# lag function Error

Hi All,

I have attached SAS Data Set, New_Gold needs to calculate as

If _n_ then .

if _n_=2 then (Obs2-Obs1)^2

if _n_=3 then (obs3-obs2)^2

if _n_=4 then (Obs4-Obs3)^2 etc

As a first step I am using Lag function.

New_Gold=Lag(1_GoldS)

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE,
GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.

ERROR 72-185: The LAG function call has too many arguments.

Accepted Solutions
Solution
‎12-21-2015 01:23 PM
Super User
Posts: 6,785

## Re: lag function Error

Seems like you could get what you want with:

new_gold = dif(GoldS)**2;

It will generate a missing value on the first observation, but that is expected.

If you are stuck on using LAG instead of DIF:

new_gold = lag(GoldS);

if _n_=1 then new_gold=.;

else new_gold = (GoldS - new_gold)**2;

Note that SAS uses ** (not ^) to indicate raising to a power.

Good luck.

All Replies
Super User
Posts: 23,771

## Re: lag function Error

The function is called is LAG<n> where <n> represents the amount of lag.

lag1(variable_name).

Variables don't typically start with numbers, so perhaps its just a typo?
Contributor
Posts: 20

## Lag function Error

Hi All,

I have data as attached. New_Gold needs to calculate as

If _n_ then .

if _n_=2 then (Obs2-Obs1)^2

if _n_=3 then (obs3-obs2)^2

if _n_=4 then (Obs4-Obs3)^2 etc

As a first step I am using Lag function.

New_Gold=Lag(1_GoldS)

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE,
GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.

ERROR 72-185: The LAG function call has too many arguments.

Super User
Posts: 23,771

## Re: Lag function Error

Do you understand my comment? If not, please post your whole code and log. The code posted above isn't valid SAS syntax. As mentioned variables rarely start with numbers (1_GoldS)
Contributor
Posts: 20

Super User
Posts: 23,771

## Re: Lag function Error

Solution
‎12-21-2015 01:23 PM
Super User
Posts: 6,785

## Re: lag function Error

Seems like you could get what you want with:

new_gold = dif(GoldS)**2;

It will generate a missing value on the first observation, but that is expected.

If you are stuck on using LAG instead of DIF:

new_gold = lag(GoldS);

if _n_=1 then new_gold=.;

else new_gold = (GoldS - new_gold)**2;

Note that SAS uses ** (not ^) to indicate raising to a power.

Good luck.

🔒 This topic is solved and locked.