How do I use the lag function or any other function for that matter... I want to convert my data from column a to column b c d depending upon the data in a :
a | b | c | d | |
1 | ||||
2 | ||||
3 | ||||
4 | 1 | 1 | ||
5 | ||||
6 | ||||
7 | 2 | 1 | ||
8 | 1 | |||
9 | ||||
10 | 2 | 1 | ||
11 | 1 | |||
12 | ||||
13 | 3 | 1 | ||
14 | 1 | |||
15 | 1 | |||
16 | ||||
17 |
Something like this?
data have;
infile cards dlm='09'x truncover;
input ID a;
cards;
1
2
3
4 1
5
6
7 2
8
9
10 2
11
12
13 3
14
15
;
data want;
set have;
retain _c _d;
if a=1 then
b=1;
if a=2 then
_c=_n_;
if _n_<=_c+1 then
c=1;
if a=3 then
_d=_N_;
if _n_<=_d+2 then
d=1;
drop _:;
run;
Haikuo
Something like this?
data have;
infile cards dlm='09'x truncover;
input ID a;
cards;
1
2
3
4 1
5
6
7 2
8
9
10 2
11
12
13 3
14
15
;
data want;
set have;
retain _c _d;
if a=1 then
b=1;
if a=2 then
_c=_n_;
if _n_<=_c+1 then
c=1;
if a=3 then
_d=_N_;
if _n_<=_d+2 then
d=1;
drop _:;
run;
Haikuo
data have; infile cards truncover; input ID a ; cards; 1 2 3 4 1 5 6 7 2 8 9 10 2 11 12 13 3 14 15 ; run; proc sql noprint; select count(a) into : n from have; quit; data want(drop=i id n); set have; array x{*} n1-n%left(&n) ; if not missing(a) then do; n+1; do i=1 to a; x{n}=1;output; a=.; end; end; else output; run;
Xia Keshan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.