09-18-2015 11:33 AM
I am using the lag function which works for parts of my data set, while it doesn't in other parts (?) I am using this very simple data step below: Does anybody know what's wrong with this code?
if count_cluster=lag1(count_cluster) then Max_Salary_previous_grade=lag1(max_salary); else Max_Salary_previous_grade=0;
09-21-2015 01:36 PM - edited 09-21-2015 01:39 PM
You can not execute the LAG function conditionally. The LAG function has to be executed for every observation.
See the sample below with the modfied logic. You can make use of BY group processing and the FIRST. functionality.
data have; infile cards dlm=","; input count_cluster Max_salary ; cards; 1, 56768 1,70741 2,49957 3,50000 4,34360 4,49790 4,53220 ; data want; set have; by count_cluster; _prev_salary = lag1(max_salary); if first.count_cluster = 0 then do; Max_Salary_previous_grade = _prev_salary; end; drop _prev_salary; run;