BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Coa_SAs
Fluorite | Level 6

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)

 

I am getting the follwing error, Please advise.

 


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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

6 REPLIES 6
Reeza
Super User
The function is called is LAG<n> where <n> represents the amount of lag.

Your code should be:
lag1(variable_name).

Variables don't typically start with numbers, so perhaps its just a typo?
Coa_SAs
Fluorite | Level 6

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)

 

I am getting the follwing error, Please advise.

 


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.

 

Reeza
Super User
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)
Coa_SAs
Fluorite | Level 6

Thanks Reeza for your quick reply, I got it.

Reeza
Super User
Please mark the question as answered.
Astounding
PROC Star

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.

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
  • 6 replies
  • 1569 views
  • 1 like
  • 3 in conversation