creating same values within a group using retain statement in SAS
up vote
1
down vote
favorite
I am trying to create a variable z that will take the same value within a group based on values of two variables X and Y of the first observation within a group. There are 4 possible values of Z a group can take based on the X and Y values of the first observation in the group.
Z=1 (if X=1 & Y=1),
Z=2 (if X=2 & Y=1),
Z=3 (if X=1 & Y=2), and
Z=4 (if X=2 & Y=2).
This is what I have and what I want.
X has two values, 1 or 2, within a group; while Y can take 1, 2 ,3.
Y is sorted in ascending order
if the first (or all group observations) take a value of 3, the resulting Z
value should be set to missing
This is what I have:
Obs Group X Y
1 10600 1 1
2 10600 1 2
3 10600 1 3
4 10800 2 1
5 10800 2 3
6 10900 1 2
7 10900 1 3
8 11100 2 2
9 11100 2 2
10 11100 2 3
11 11100 2 2
12 11200 2 3
13 11300 2 1
14 11300 2 2
15 11300 1 3
16 11300 1 3
17 11300 1 3
18 11300 1 3
And here is what I want:
Obs Group X Y Z
1 10600 1 1 1
2 10600 1 2 1
3 10600 1 3 1
4 10800 2 1 2
5 10800 2 3 2
6 10900 1 2 3
7 10900 1 3 3
8 11100 2 2 4
9 11100 2 2 4
10 11100 2 3 4
11 11100 2 2 4
12 11200 2 3 .
13 11300 2 1 2
14 11300 2 2 2
15 11300 1 3 .
16 11300 1 3 .
17 11300 1 3 .
18 11300 1 3 .
Thank you!
sas retain
add a comment |
up vote
1
down vote
favorite
I am trying to create a variable z that will take the same value within a group based on values of two variables X and Y of the first observation within a group. There are 4 possible values of Z a group can take based on the X and Y values of the first observation in the group.
Z=1 (if X=1 & Y=1),
Z=2 (if X=2 & Y=1),
Z=3 (if X=1 & Y=2), and
Z=4 (if X=2 & Y=2).
This is what I have and what I want.
X has two values, 1 or 2, within a group; while Y can take 1, 2 ,3.
Y is sorted in ascending order
if the first (or all group observations) take a value of 3, the resulting Z
value should be set to missing
This is what I have:
Obs Group X Y
1 10600 1 1
2 10600 1 2
3 10600 1 3
4 10800 2 1
5 10800 2 3
6 10900 1 2
7 10900 1 3
8 11100 2 2
9 11100 2 2
10 11100 2 3
11 11100 2 2
12 11200 2 3
13 11300 2 1
14 11300 2 2
15 11300 1 3
16 11300 1 3
17 11300 1 3
18 11300 1 3
And here is what I want:
Obs Group X Y Z
1 10600 1 1 1
2 10600 1 2 1
3 10600 1 3 1
4 10800 2 1 2
5 10800 2 3 2
6 10900 1 2 3
7 10900 1 3 3
8 11100 2 2 4
9 11100 2 2 4
10 11100 2 3 4
11 11100 2 2 4
12 11200 2 3 .
13 11300 2 1 2
14 11300 2 2 2
15 11300 1 3 .
16 11300 1 3 .
17 11300 1 3 .
18 11300 1 3 .
Thank you!
sas retain
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to create a variable z that will take the same value within a group based on values of two variables X and Y of the first observation within a group. There are 4 possible values of Z a group can take based on the X and Y values of the first observation in the group.
Z=1 (if X=1 & Y=1),
Z=2 (if X=2 & Y=1),
Z=3 (if X=1 & Y=2), and
Z=4 (if X=2 & Y=2).
This is what I have and what I want.
X has two values, 1 or 2, within a group; while Y can take 1, 2 ,3.
Y is sorted in ascending order
if the first (or all group observations) take a value of 3, the resulting Z
value should be set to missing
This is what I have:
Obs Group X Y
1 10600 1 1
2 10600 1 2
3 10600 1 3
4 10800 2 1
5 10800 2 3
6 10900 1 2
7 10900 1 3
8 11100 2 2
9 11100 2 2
10 11100 2 3
11 11100 2 2
12 11200 2 3
13 11300 2 1
14 11300 2 2
15 11300 1 3
16 11300 1 3
17 11300 1 3
18 11300 1 3
And here is what I want:
Obs Group X Y Z
1 10600 1 1 1
2 10600 1 2 1
3 10600 1 3 1
4 10800 2 1 2
5 10800 2 3 2
6 10900 1 2 3
7 10900 1 3 3
8 11100 2 2 4
9 11100 2 2 4
10 11100 2 3 4
11 11100 2 2 4
12 11200 2 3 .
13 11300 2 1 2
14 11300 2 2 2
15 11300 1 3 .
16 11300 1 3 .
17 11300 1 3 .
18 11300 1 3 .
Thank you!
sas retain
I am trying to create a variable z that will take the same value within a group based on values of two variables X and Y of the first observation within a group. There are 4 possible values of Z a group can take based on the X and Y values of the first observation in the group.
Z=1 (if X=1 & Y=1),
Z=2 (if X=2 & Y=1),
Z=3 (if X=1 & Y=2), and
Z=4 (if X=2 & Y=2).
This is what I have and what I want.
X has two values, 1 or 2, within a group; while Y can take 1, 2 ,3.
Y is sorted in ascending order
if the first (or all group observations) take a value of 3, the resulting Z
value should be set to missing
This is what I have:
Obs Group X Y
1 10600 1 1
2 10600 1 2
3 10600 1 3
4 10800 2 1
5 10800 2 3
6 10900 1 2
7 10900 1 3
8 11100 2 2
9 11100 2 2
10 11100 2 3
11 11100 2 2
12 11200 2 3
13 11300 2 1
14 11300 2 2
15 11300 1 3
16 11300 1 3
17 11300 1 3
18 11300 1 3
And here is what I want:
Obs Group X Y Z
1 10600 1 1 1
2 10600 1 2 1
3 10600 1 3 1
4 10800 2 1 2
5 10800 2 3 2
6 10900 1 2 3
7 10900 1 3 3
8 11100 2 2 4
9 11100 2 2 4
10 11100 2 3 4
11 11100 2 2 4
12 11200 2 3 .
13 11300 2 1 2
14 11300 2 2 2
15 11300 1 3 .
16 11300 1 3 .
17 11300 1 3 .
18 11300 1 3 .
Thank you!
sas retain
sas retain
edited Nov 9 at 22:30
Richard
7,31821125
7,31821125
asked Nov 9 at 17:24
stunt
156
156
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You are correct that a retain
ed variable will carry a value into forward iterations of the data step. Nominally, a simple data step with a single set
statement an iteration will correspond to a row in the data set.
Your retained variable is to be assigned at the start of a group, so you will need a by
statement, which in turn makes an automatic flag variable first.<by-group-var>
available.
data have; input
Group X Y; datalines;
10600 1 1
10600 1 2
10600 1 3
10800 2 1
10800 2 3
10900 1 2
10900 1 3
11100 2 2
11100 2 2
11100 2 3
11100 2 2
11200 2 3
11300 2 1
11300 2 2
11300 1 3
11300 1 3
11300 1 3
11300 1 3
run;
The last set of rows with group=11300
have x=2
followed by x=1
. Your narrative
within a group
conveys an idea but is not explicitly precise. The actual grouping (based on the shown want) appears to be a combination of group
and x
. Thus, you will need a
by group x notsorted;
statement. The notsorted
will cause the data step setup the first.
and last.
based on continguity of the values instead of the explicit ordering of values.
data want;
set have;
by group x nostsorted;
retain z;
if first.x then do; * detect first row in combinations "group/x";
select;
when (X=1 & Y=1) Z=1; * apply logic for retained value;
when (X=2 & Y=1) Z=2;
when (X=1 & Y=2) Z=3;
when (X=2 & Y=2) Z=4;
otherwise Z=.;
end;
end;
logic_tracker_first_x = first.x;
run;
ods listing; options nocenter;
proc print data=want;
run;
The output window shows
logic_tracker_
Obs Group X Y z first_x
1 10600 1 1 1 1
2 10600 1 2 1 0
3 10600 1 3 1 0
4 10800 2 1 2 1
5 10800 2 3 2 0
6 10900 1 2 3 1
7 10900 1 3 3 0
8 11100 2 2 4 1
9 11100 2 2 4 0
10 11100 2 3 4 0
11 11100 2 2 4 0
12 11200 2 3 . 1
13 11300 2 1 2 1
14 11300 2 2 2 0
15 11300 1 3 . 1
16 11300 1 3 . 0
17 11300 1 3 . 0
18 11300 1 3 . 0
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon afterotherwise
, but that should have logged anERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.
– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
add a comment |
up vote
1
down vote
please try using the following solution , I have used simpler approach by keeping only First Z variable per Group and then did a left join with same dataset to keep the First z variable across remaining observations for same group-
data test;
input group 5. x 1. y 1.;
if x=1 and y=1 then z=1;
else if x=2 and y=1 then z=2;
else if x=1 and y=2 then z=3;
else if x=2 and y=2 then z=4;
datalines;
1060011
1060012
1060013
1080021
1080023
1090012
1090013
1110022
1110022
1110023
1110022
1120023
1130021
1130022
1130013
1130013
1130013
1130013
;
run;
data test1;
set test;
keep group x z;
run;
proc sort data=test1; by group x; run;
data keep_first;
set test1;
by group x;
if first.group or first.x;
run;
proc sql;
create table final
as
select a.group, a.x, a.y, b.z
from test a
left join keep_first b
on a.group=b.group
and a.x=b.x
order by a.group, a.y, a.x;
quit;
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
|
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You are correct that a retain
ed variable will carry a value into forward iterations of the data step. Nominally, a simple data step with a single set
statement an iteration will correspond to a row in the data set.
Your retained variable is to be assigned at the start of a group, so you will need a by
statement, which in turn makes an automatic flag variable first.<by-group-var>
available.
data have; input
Group X Y; datalines;
10600 1 1
10600 1 2
10600 1 3
10800 2 1
10800 2 3
10900 1 2
10900 1 3
11100 2 2
11100 2 2
11100 2 3
11100 2 2
11200 2 3
11300 2 1
11300 2 2
11300 1 3
11300 1 3
11300 1 3
11300 1 3
run;
The last set of rows with group=11300
have x=2
followed by x=1
. Your narrative
within a group
conveys an idea but is not explicitly precise. The actual grouping (based on the shown want) appears to be a combination of group
and x
. Thus, you will need a
by group x notsorted;
statement. The notsorted
will cause the data step setup the first.
and last.
based on continguity of the values instead of the explicit ordering of values.
data want;
set have;
by group x nostsorted;
retain z;
if first.x then do; * detect first row in combinations "group/x";
select;
when (X=1 & Y=1) Z=1; * apply logic for retained value;
when (X=2 & Y=1) Z=2;
when (X=1 & Y=2) Z=3;
when (X=2 & Y=2) Z=4;
otherwise Z=.;
end;
end;
logic_tracker_first_x = first.x;
run;
ods listing; options nocenter;
proc print data=want;
run;
The output window shows
logic_tracker_
Obs Group X Y z first_x
1 10600 1 1 1 1
2 10600 1 2 1 0
3 10600 1 3 1 0
4 10800 2 1 2 1
5 10800 2 3 2 0
6 10900 1 2 3 1
7 10900 1 3 3 0
8 11100 2 2 4 1
9 11100 2 2 4 0
10 11100 2 3 4 0
11 11100 2 2 4 0
12 11200 2 3 . 1
13 11300 2 1 2 1
14 11300 2 2 2 0
15 11300 1 3 . 1
16 11300 1 3 . 0
17 11300 1 3 . 0
18 11300 1 3 . 0
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon afterotherwise
, but that should have logged anERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.
– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
add a comment |
up vote
1
down vote
accepted
You are correct that a retain
ed variable will carry a value into forward iterations of the data step. Nominally, a simple data step with a single set
statement an iteration will correspond to a row in the data set.
Your retained variable is to be assigned at the start of a group, so you will need a by
statement, which in turn makes an automatic flag variable first.<by-group-var>
available.
data have; input
Group X Y; datalines;
10600 1 1
10600 1 2
10600 1 3
10800 2 1
10800 2 3
10900 1 2
10900 1 3
11100 2 2
11100 2 2
11100 2 3
11100 2 2
11200 2 3
11300 2 1
11300 2 2
11300 1 3
11300 1 3
11300 1 3
11300 1 3
run;
The last set of rows with group=11300
have x=2
followed by x=1
. Your narrative
within a group
conveys an idea but is not explicitly precise. The actual grouping (based on the shown want) appears to be a combination of group
and x
. Thus, you will need a
by group x notsorted;
statement. The notsorted
will cause the data step setup the first.
and last.
based on continguity of the values instead of the explicit ordering of values.
data want;
set have;
by group x nostsorted;
retain z;
if first.x then do; * detect first row in combinations "group/x";
select;
when (X=1 & Y=1) Z=1; * apply logic for retained value;
when (X=2 & Y=1) Z=2;
when (X=1 & Y=2) Z=3;
when (X=2 & Y=2) Z=4;
otherwise Z=.;
end;
end;
logic_tracker_first_x = first.x;
run;
ods listing; options nocenter;
proc print data=want;
run;
The output window shows
logic_tracker_
Obs Group X Y z first_x
1 10600 1 1 1 1
2 10600 1 2 1 0
3 10600 1 3 1 0
4 10800 2 1 2 1
5 10800 2 3 2 0
6 10900 1 2 3 1
7 10900 1 3 3 0
8 11100 2 2 4 1
9 11100 2 2 4 0
10 11100 2 3 4 0
11 11100 2 2 4 0
12 11200 2 3 . 1
13 11300 2 1 2 1
14 11300 2 2 2 0
15 11300 1 3 . 1
16 11300 1 3 . 0
17 11300 1 3 . 0
18 11300 1 3 . 0
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon afterotherwise
, but that should have logged anERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.
– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You are correct that a retain
ed variable will carry a value into forward iterations of the data step. Nominally, a simple data step with a single set
statement an iteration will correspond to a row in the data set.
Your retained variable is to be assigned at the start of a group, so you will need a by
statement, which in turn makes an automatic flag variable first.<by-group-var>
available.
data have; input
Group X Y; datalines;
10600 1 1
10600 1 2
10600 1 3
10800 2 1
10800 2 3
10900 1 2
10900 1 3
11100 2 2
11100 2 2
11100 2 3
11100 2 2
11200 2 3
11300 2 1
11300 2 2
11300 1 3
11300 1 3
11300 1 3
11300 1 3
run;
The last set of rows with group=11300
have x=2
followed by x=1
. Your narrative
within a group
conveys an idea but is not explicitly precise. The actual grouping (based on the shown want) appears to be a combination of group
and x
. Thus, you will need a
by group x notsorted;
statement. The notsorted
will cause the data step setup the first.
and last.
based on continguity of the values instead of the explicit ordering of values.
data want;
set have;
by group x nostsorted;
retain z;
if first.x then do; * detect first row in combinations "group/x";
select;
when (X=1 & Y=1) Z=1; * apply logic for retained value;
when (X=2 & Y=1) Z=2;
when (X=1 & Y=2) Z=3;
when (X=2 & Y=2) Z=4;
otherwise Z=.;
end;
end;
logic_tracker_first_x = first.x;
run;
ods listing; options nocenter;
proc print data=want;
run;
The output window shows
logic_tracker_
Obs Group X Y z first_x
1 10600 1 1 1 1
2 10600 1 2 1 0
3 10600 1 3 1 0
4 10800 2 1 2 1
5 10800 2 3 2 0
6 10900 1 2 3 1
7 10900 1 3 3 0
8 11100 2 2 4 1
9 11100 2 2 4 0
10 11100 2 3 4 0
11 11100 2 2 4 0
12 11200 2 3 . 1
13 11300 2 1 2 1
14 11300 2 2 2 0
15 11300 1 3 . 1
16 11300 1 3 . 0
17 11300 1 3 . 0
18 11300 1 3 . 0
You are correct that a retain
ed variable will carry a value into forward iterations of the data step. Nominally, a simple data step with a single set
statement an iteration will correspond to a row in the data set.
Your retained variable is to be assigned at the start of a group, so you will need a by
statement, which in turn makes an automatic flag variable first.<by-group-var>
available.
data have; input
Group X Y; datalines;
10600 1 1
10600 1 2
10600 1 3
10800 2 1
10800 2 3
10900 1 2
10900 1 3
11100 2 2
11100 2 2
11100 2 3
11100 2 2
11200 2 3
11300 2 1
11300 2 2
11300 1 3
11300 1 3
11300 1 3
11300 1 3
run;
The last set of rows with group=11300
have x=2
followed by x=1
. Your narrative
within a group
conveys an idea but is not explicitly precise. The actual grouping (based on the shown want) appears to be a combination of group
and x
. Thus, you will need a
by group x notsorted;
statement. The notsorted
will cause the data step setup the first.
and last.
based on continguity of the values instead of the explicit ordering of values.
data want;
set have;
by group x nostsorted;
retain z;
if first.x then do; * detect first row in combinations "group/x";
select;
when (X=1 & Y=1) Z=1; * apply logic for retained value;
when (X=2 & Y=1) Z=2;
when (X=1 & Y=2) Z=3;
when (X=2 & Y=2) Z=4;
otherwise Z=.;
end;
end;
logic_tracker_first_x = first.x;
run;
ods listing; options nocenter;
proc print data=want;
run;
The output window shows
logic_tracker_
Obs Group X Y z first_x
1 10600 1 1 1 1
2 10600 1 2 1 0
3 10600 1 3 1 0
4 10800 2 1 2 1
5 10800 2 3 2 0
6 10900 1 2 3 1
7 10900 1 3 3 0
8 11100 2 2 4 1
9 11100 2 2 4 0
10 11100 2 3 4 0
11 11100 2 2 4 0
12 11200 2 3 . 1
13 11300 2 1 2 1
14 11300 2 2 2 0
15 11300 1 3 . 1
16 11300 1 3 . 0
17 11300 1 3 . 0
18 11300 1 3 . 0
edited Nov 11 at 16:37
answered Nov 9 at 22:14
Richard
7,31821125
7,31821125
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon afterotherwise
, but that should have logged anERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.
– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
add a comment |
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon afterotherwise
, but that should have logged anERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.
– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
Thanks for you response. I tried this code but it gives missing values after the first within each group. Can you attempt it using the have data and see if it works. It may need a bi of tweaking. Thanks!
– stunt
Nov 11 at 15:07
See edit, seems to work. Original was missing semi-colon after
otherwise
, but that should have logged an ERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.– Richard
Nov 11 at 15:23
See edit, seems to work. Original was missing semi-colon after
otherwise
, but that should have logged an ERROR 22-322: Syntax error, expecting one of the following:...
Perhaps you got the error and did not look at the log and was looking at the data output from a prior submit.– Richard
Nov 11 at 15:23
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
Ok thanks, that works. I think the group x notsorted is what was needed as well. Thanks very much for your help.
– stunt
Nov 11 at 21:02
add a comment |
up vote
1
down vote
please try using the following solution , I have used simpler approach by keeping only First Z variable per Group and then did a left join with same dataset to keep the First z variable across remaining observations for same group-
data test;
input group 5. x 1. y 1.;
if x=1 and y=1 then z=1;
else if x=2 and y=1 then z=2;
else if x=1 and y=2 then z=3;
else if x=2 and y=2 then z=4;
datalines;
1060011
1060012
1060013
1080021
1080023
1090012
1090013
1110022
1110022
1110023
1110022
1120023
1130021
1130022
1130013
1130013
1130013
1130013
;
run;
data test1;
set test;
keep group x z;
run;
proc sort data=test1; by group x; run;
data keep_first;
set test1;
by group x;
if first.group or first.x;
run;
proc sql;
create table final
as
select a.group, a.x, a.y, b.z
from test a
left join keep_first b
on a.group=b.group
and a.x=b.x
order by a.group, a.y, a.x;
quit;
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
|
show 2 more comments
up vote
1
down vote
please try using the following solution , I have used simpler approach by keeping only First Z variable per Group and then did a left join with same dataset to keep the First z variable across remaining observations for same group-
data test;
input group 5. x 1. y 1.;
if x=1 and y=1 then z=1;
else if x=2 and y=1 then z=2;
else if x=1 and y=2 then z=3;
else if x=2 and y=2 then z=4;
datalines;
1060011
1060012
1060013
1080021
1080023
1090012
1090013
1110022
1110022
1110023
1110022
1120023
1130021
1130022
1130013
1130013
1130013
1130013
;
run;
data test1;
set test;
keep group x z;
run;
proc sort data=test1; by group x; run;
data keep_first;
set test1;
by group x;
if first.group or first.x;
run;
proc sql;
create table final
as
select a.group, a.x, a.y, b.z
from test a
left join keep_first b
on a.group=b.group
and a.x=b.x
order by a.group, a.y, a.x;
quit;
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
|
show 2 more comments
up vote
1
down vote
up vote
1
down vote
please try using the following solution , I have used simpler approach by keeping only First Z variable per Group and then did a left join with same dataset to keep the First z variable across remaining observations for same group-
data test;
input group 5. x 1. y 1.;
if x=1 and y=1 then z=1;
else if x=2 and y=1 then z=2;
else if x=1 and y=2 then z=3;
else if x=2 and y=2 then z=4;
datalines;
1060011
1060012
1060013
1080021
1080023
1090012
1090013
1110022
1110022
1110023
1110022
1120023
1130021
1130022
1130013
1130013
1130013
1130013
;
run;
data test1;
set test;
keep group x z;
run;
proc sort data=test1; by group x; run;
data keep_first;
set test1;
by group x;
if first.group or first.x;
run;
proc sql;
create table final
as
select a.group, a.x, a.y, b.z
from test a
left join keep_first b
on a.group=b.group
and a.x=b.x
order by a.group, a.y, a.x;
quit;
please try using the following solution , I have used simpler approach by keeping only First Z variable per Group and then did a left join with same dataset to keep the First z variable across remaining observations for same group-
data test;
input group 5. x 1. y 1.;
if x=1 and y=1 then z=1;
else if x=2 and y=1 then z=2;
else if x=1 and y=2 then z=3;
else if x=2 and y=2 then z=4;
datalines;
1060011
1060012
1060013
1080021
1080023
1090012
1090013
1110022
1110022
1110023
1110022
1120023
1130021
1130022
1130013
1130013
1130013
1130013
;
run;
data test1;
set test;
keep group x z;
run;
proc sort data=test1; by group x; run;
data keep_first;
set test1;
by group x;
if first.group or first.x;
run;
proc sql;
create table final
as
select a.group, a.x, a.y, b.z
from test a
left join keep_first b
on a.group=b.group
and a.x=b.x
order by a.group, a.y, a.x;
quit;
answered Nov 9 at 22:27
Rhythm
635
635
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
|
show 2 more comments
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
There are no x and y in your first step's datalines.
– Richard
Nov 9 at 22:33
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
yes .. there are .. 1060011 "10600 1 1" I didn't mention any spaces , so my informat also read 5., 1., 1.
– Rhythm
Nov 9 at 22:34
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Ah, see it now, old eyes. Apologies
– Richard
Nov 10 at 1:06
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Thanks for you response. How do I maintain (or bring back) other variables in the dataset apart from group, x, y &z?
– stunt
Nov 11 at 15:10
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
Ok thanks for this interesting approach using sql to create a table and ten left join. works. Thank you!
– stunt
Nov 11 at 21:07
|
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230571%2fcreating-same-values-within-a-group-using-retain-statement-in-sas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown