This program starts by creating a 't1' dataset with hardcoded values. It then sorts this data by the 'x' variable. A subsequent Data Step ('t2') uses the LAG function to create a new variable 'z' containing the previous value of 'y'. Finally, a last step ('t3') applies complex conditional logic using comparisons between current and lagged values to modify variables.
Data Analysis
Type : CREATION_INTERNE
Data is created directly in the code via the CARDS statement in the data t1 step.
1 Code Block
DATA STEP Data
Explanation : Creation of table 't1' with two numeric variables x and y, populated by internal data (CARDS).
Copied!
data t1;
input x y;
cards;
1 5
2 6
3 6
7 9
8 10
11 12
;
run;
1
DATA t1;
2
INPUT x y;
3
CARDS;
4
15
5
26
6
36
7
79
8
810
9
1112
10
;
11
RUN;
2 Code Block
PROC SORT
Explanation : Sorting table 't1' in ascending order by variable 'x'.
Copied!
proc sort data=t1; by x; run;
1
PROC SORTDATA=t1; BY x; RUN;
3 Code Block
DATA STEP Data
Explanation : Creation of table 't2' from 't1'. The LAG(y) function is used to retrieve the value of 'y' from the previous observation and store it in 'z'.
Copied!
data t2;
set t1 end=b;
z=lag(y);
/*if x>z or b;
proc sort; by decending x;
*/
run;
1
DATA t2;
2
SET t1 END=b;
3
z=lag(y);
4
/*if x>z or b;
5
proc sort; by decending x;
6
*/
7
RUN;
4 Code Block
PROC PRINT
Explanation : Displaying the content of the last created table (t2) in the standard output.
Copied!
proc print;run;
1
PROC PRINT;RUN;
5 Code Block
DATA STEP Data
Explanation : Creation of table 't3' from 't2'. Applies conditional modifications: if x is less than z, z takes the value of y. Then, y is updated with the maximum between its current value and the previous value of z.
Copied!
data t3;
set t2;
if x<z then z=y;
y=max(y,lag(z));
*if y^=z;
run;
1
DATA t3;
2
SET t2;
3
IF xTHEN z=y;
4
y=max(y,lag(z));
5
*if y^=z;
6
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.