Help using Base SAS procedures

Calculation number of different observations

Reply
Occasional Contributor
Posts: 7

Calculation number of different observations

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
Occasional Contributor
Posts: 7

Re: Calculation number of different observations

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;
New Contributor
Posts: 3

Re: Calculation number of different observations

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.*/
Valued Guide
Posts: 2,175

Re: Calculation number of different observations

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.*/
PROC Star
Posts: 7,363

Re: Calculation number of different observations

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
Ask a Question
Discussion stats
  • 4 replies
  • 100 views
  • 0 likes
  • 4 in conversation