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-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!

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.

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
  • 4 replies
  • 629 views
  • 0 likes
  • 4 in conversation