7 Using the lag function, create an indicator variable, PY_RESTATEMENT, coded 1 if the company issued a restatement in the prior year, and coded 0 otherwise.
data sql.Firm_data; set sql.Firm_data;
py_restatement=lag(restatement);
if restatement ne lag(restatement) then py_restatement=1;
else py_restatement=0;
run;
however this isn't working, any ideas?
What does "issued a restatement in the prior year" mean?
What does the RESTATEMENT variable contain?
How many firms are in the dataset?
Is the dataset order by firm and year?
Are there any missing years?
Please share some sample data with expected results.
Hint: Do not overwrite your input dataset, that will make debugging impossible because you no longer have the original input.
Try one of these.
If RESTATEMENT is already a 1/0 (true false) flag variable.
data want;
set have;
py_restatement=lag(restatement);
run;
If RESTATEMENT having a non-zero or non-missing value means there was a restatement.
data want;
set have;
py_restatement= not not lag(restatement);
run;
So here are the prior steps that led to this:
Basically I want to lag when restatement = 1 or 0. However I, unfortunately, have to use the lag statement to get full credit. I don't know how to lag, really, so the last 3 statements are impossible for me.
I don't think the LAG() part is really that hard for this.
Your step 3 is really just the code I showed before. If you have already created RESTATEMENT as 0/1 flag variable then the simple LAG(RESTATEMENT) will get you the value of RESTATEMENT from the previous observation. The key thing is that the data needs to be ordered properly, otherwise the previous value of restatement is not the previous year's value.
But you need to take into account that you have multiple company. You don't want the flag on the first observation for a company to have the value from the last observation for the previous company.
Let's use COMPANY and YEAR as the variables that define the company and the year.
data want;
set have;
by company year ;
py_restatement=lag(restatement);
if first.company then py_restatement=0;
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.