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.
Maxim 1: Read the Documentation.
From BY Statement in Data Step Programming / Data Step Statements:
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.
What happens if you execute the code?
See Maxim 4 😉
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.
Maxim 1: Read the Documentation.
From BY Statement in Data Step Programming / Data Step Statements:
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.