Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Calculation number of different observations

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-31-2011 08:21 AM
(732 views)

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

data experience;

set experience;

if analys ne lag(analys) then do;

experience=1;

totaal=1;

end;

else experience=totaal;

totaal+1;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.*/

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.*/

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.*/

> 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.*/

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.