BookmarkSubscribeRSS Feed
Greek
Obsidian | Level 7

Hello,

 

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?

 

Thank you!

 

data adjustment3;
set adjustment3;
if count_cluster=lag1(count_cluster) then Max_Salary_previous_grade=lag1(max_salary); else Max_Salary_previous_grade=0;

run;

 

    TABLE GET  
count_cluster Max_salary Max_Salary_previous_grade  
1            56,768                                                      -    
1            70,741                                            56,768 ok!
2            49,957                                                      -    
3            50,000                                                      -    
4            34,360                                                      -    
4            49,790                                            70,741 Error!
4            53,220                                            49,790 ok!
       
       
    TABLE WANT  
count_cluster Max_salary Max_Salary_previous_grade  
1            56,768                                                      -    
1            70,741                                            56,768  
2            49,957                                                      -    
3            50,000                                                      -    
4            34,360                                                      -    
4            49,790                                            34,360  
4            53,220                                            49,790  

 

 

 

1 REPLY 1
BrunoMueller
SAS Super FREQ

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;

Bruno

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!

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
  • 1 reply
  • 639 views
  • 0 likes
  • 2 in conversation