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

Hi SAS Experts,

 

I have the code below

data var_cal;
	set my.keepvar_1999_2001_1;
	gviidkey=catx('_',gvkey,iid);
	prccd_abs= abs(prccd);
by gviidkey; lagprccd_abs=ifn(first.gviidkey,.,lag(prccd_abs)); lagajexdi=ifn(first.gviidkey,.,lag(ajexdi)); lagtrfd=ifn(first.gviidkey,.,lag(trfd)); If n(prccd_abs,ajexdi,trfd,lagprccd_abs,lagajexdi,lagtrfd)=6 then raw_return=((prccd_abs/ajexdi*trfd)/(lagprccd_abs/ladajexdi*lagtrfd)-1)*100; log_raw_return=log(raw_return); run;

I am not sure whether it is accepted that I can use BY gviidkey when this variable was calculated right above as noted in the code.

 

Kind regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Maxim 1: Read the Documentation.

From BY Statement in Data Step Programming / Data Step Statements:

Required Argument

variable

names each variable by which the data set is sorted or indexed.

 

This implies that the variable is present in the input dataset(s).

 

Next, Maxim 4: Try It.

See this simple log piece:

 73         data want;
 74         set sashelp.class;
 75         check = name !! sex;
 76         by check;
 77         run;
 
 ERROR: BY variable check is not on input data set SASHELP.CLASS.

Your code would only run without an ERROR if gviidkey was already present in the input dataset, and it would positively not do what you expect.

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

What happens if you execute the code?

See Maxim 4 😉

Phil_NZ
Barite | Level 11

Thank you very much @andreas_lds  and @Kurt_Bremser 

 

Not only about the problem solving, but also about leading me to Maxim (previously I read a couple of discussion in this forum and catches this word sometimes but do not know what they are and think maybe it is the specific word for this topic", Now after your reply, I searched for a link and, wow, it is so cool to learn.

https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2019/3062-2019.pdf

 

Thank you @Kurt_Bremser  and other experts for delivering us such an amazing document.

Best regards.

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Kurt_Bremser
Super User

Maxim 1: Read the Documentation.

From BY Statement in Data Step Programming / Data Step Statements:

Required Argument

variable

names each variable by which the data set is sorted or indexed.

 

This implies that the variable is present in the input dataset(s).

 

Next, Maxim 4: Try It.

See this simple log piece:

 73         data want;
 74         set sashelp.class;
 75         check = name !! sex;
 76         by check;
 77         run;
 
 ERROR: BY variable check is not on input data set SASHELP.CLASS.

Your code would only run without an ERROR if gviidkey was already present in the input dataset, and it would positively not do what you expect.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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