BookmarkSubscribeRSS Feed
jebuske
Calcite | Level 5
Hi,

I've created an example in paint of how my dataset should look like (made a mistake with analys code 000001, the experience should of course go to 4 instead of 5):
http://img850.imageshack.us/img850/696/exampled.png


At this moment I'm having the timeID and Analys variables and I should calculate the experience.

I was thinking about something like this:

data experience;
set experience;
if analys ne lag(analys) then do;
experience=1;
end;
else experience=experience+1;
run;

The problem however is that it only puts a 1 on the first row when the analystcode changes but the other observations give experience=.

Anyone knows what I'm doing wrong here?


Thanks a lot! Message was edited by: jebuske
4 REPLIES 4
jebuske
Calcite | Level 5
I just found a solution. I believe it should be working correctly.

data experience;
set experience;
if analys ne lag(analys) then do;
experience=1;
totaal=1;
end;
else experience=totaal;
totaal+1;
run;
Benjy
Calcite | Level 5
I suggest using the retain statement.
DATA;
RETAIN N ; /* N is set to missing and will be retained in each subsequent iteration */
IF _N_=1 THEN N=0 ; /*first case N=0. All other cases N is still missing */
N=SUM(N, +1) ; /*FOR FIRST CASE N=0+1. The value of 1 is carried over to the second case and then 1 is added to it so the second case becomes 2. For the third case the value of 2 is retained, i.e. carries over, and 1 is added to this so N for the 3rd case is 3. And so on.*/
/* Using N=N+1 would work here as well as N=SUM(N, 1) but the latter form is good practice because it insures that a missing value for N will not make the result of the addition null or missing.*/
Peter_C
Rhodochrosite | Level 12
Benjy has the right idea but hope I can add a little value
> use RETAIN.

RETAIN N 0 ; /* N is retained but starts with 0 */
* so then don't need the line IF _N_=1 THEN N=0 ;
N +1 ; /* accumulate N each time through this statemen.*/
art297
Opal | Level 21
Jebuske,

I probably don't understand what you want but, if I do, I think you can get away with something like:

data experience;
set experience;
by analys notsorted;
if first.analys then experience=1;
else experience+1;
run;

The by 'analysis notsorted' is just the way I like to code .. it has the same effect as your use of lag(). experience is automatically retained because of your use of the form experience+1.

HTH,
Art

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1339 views
  • 0 likes
  • 4 in conversation