BookmarkSubscribeRSS Feed
1SasUser1
Calcite | Level 5

Hi,

 

I am using SAS Enterprise Guide 5.1 with the following code:

 

data work.test;
set WORK.QUERY;

by Number;

if first.Number then Recovered = .;
else if lag(Level) > Level then Recovered = 1; else Recovered = 0;
if first.Number then Rolled = .;
else if lag(Level) < Level then Rolled = 1; else Rolled = 0;
if first.Number then Stable = .;
else if lag(Level) = Level then Stable = 1; else Stable = 0;
run;

 

I am trying to count the number of occasions a person rolls through a level and how many times they recover. I have been using the LAG function but it seems to check the row before which could be a different person. I tried setting the first.person = . and then starting my IF function, but it shifted everything down.

 

I tried it for one person before the full data set, but it didn't work. Hopefully below shows it clearer with what I want:

 

 Columns to use Columns to use What I want it to showWhat I want it to showWhat I want it to showWhat it showsWhat it showsWhat it shows
NumberLevelRolledRecoveredStableRolledRecoveredStable
123000000...   
123000000001   
123000001100   
123000000010   
123000001100   
123000002000   
123000003000   
123000004000   
123000005000   
456000000......
456000000001100
456000000001001
456000001100100
456000000010010
456000000001001
456000000001001
456000000001001
456000000001001
456000000001001
456000000001001
456000000001001
789000000...   
789000000001   
789000001100   
789000000010   
789000001100   
789000000010   
789000001100   
789000001001   
789000002100   
789000003000   
789000003001   
789000004100   

 

As you can see when the level goes from 0-5 consecutively then it should only be counted as 1 roll. If the previous row has the same value e.g. 0 then 0, then it is stable. If the previous row has a higher level then it is counted as a recover.

 

Any help would be greatly appreciated!

6 REPLIES 6
andreas_lds
Jade | Level 19

Afaik calling lag three times generates three value lists and you need to call lag when first.Number so that the first value is stored in the list. Try:

 

data work.test;
   set WORK.QUERY;
   by Number;

   laggedLevel = lag(Level);

   if first.Number then do;
      call missing(Recovered, Rolled, Stable);
   end;
   else do;      
      Recovered = (laggedLevel > Level);
      Rolled =  (laggedLevel < Level);
      Stable =  (laggedLevel = Level);
   end;

run;

Why are you creating three variables? Maybe creating one variable with three different values allows easier usage of the data.

1SasUser1
Calcite | Level 5

Thank you for responding. Your code works perfect for the most part. My only bit is when the level increases. So the part when it goes 1, 2, 3, 4, 5. Is there a way to just make it say 1 at the first increase, rather than the column Rolled having 1 next to each higher level?

 

 What it shows with your codeWhat I want it to show
LevelRolledRolled
0..
000
111
000
111
210
310
410
510

 

The reason why there are 3 separate columns is so that afterwards I can sum the number of rolls per person.

 

Thanks again.

andreas_lds
Jade | Level 19

Should be possible, but i can't post tested code without data to work with, so please post data in usable form: a data step using datalines. See How to convert datasets to data steps for details.

1SasUser1
Calcite | Level 5

Thanks for that link, however I get the following error message: ERROR: The connection has timed out..
ERROR: Cannot open %INCLUDE file REPREX. ERROR 180-322: Statement is not valid or it is used out of proper order.

 

I have attached an Excel file, are you able to import that?

andreas_lds
Jade | Level 19

@1SasUser1 wrote:

Thanks for that link, however I get the following error message: ERROR: The connection has timed out..
ERROR: Cannot open %INCLUDE file REPREX. ERROR 180-322: Statement is not valid or it is used out of proper order.

 

I have attached an Excel file, are you able to import that?


No, i can't open excel files.

1SasUser1
Calcite | Level 5

Hi,

 

I think I managed it:

 

data WORK.SAMPLEDATA;
infile datalines dsd truncover;
input Number:BEST12. Level:BEST12.;
format Number BEST12. Level BEST12.;
datalines;
12300000 0
12300000 0
12300000 1
12300000 0
12300000 1
12300000 2
12300000 3
12300000 4
12300000 5
45600000 0
45600000 0
45600000 0
45600000 1
45600000 0
45600000 0
45600000 0
45600000 0
45600000 0
45600000 0
45600000 0
45600000 0
78900000 0
78900000 0
78900000 1
78900000 0
78900000 1
78900000 0
78900000 1
78900000 1
78900000 2
78900000 3
78900000 3
78900000 4
;;;;

 

Hope this works. Thanks again.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 924 views
  • 0 likes
  • 2 in conversation