Hi,
I have 4 variables as var1…var4. I want to identify the observations where same value occurs in more than 1 variable and then keep the value at it’s first occurrence.
For example,
The dataset I have:
Var1 var2 var3 var4
1 2 3 4
0 0 4 9
1 1 1 20
0 1 2 2
1 0 2 1
5 9 9 9
3 3 3 3
The dataset I want:
Var1 var2 var3 var4
1 2 3 4
0 . 4 9
1 1 . 20
0 1 2 .
1 0 2 .
5 9 . .
3 . . .
data have;
input Var1 var2 var3 var4;
cards;
1 2 3 4
0 0 4 9
1 1 1 20
0 1 2 2
1 0 2 1
5 9 9 9
3 3 3 3
;
data want;
set have;
array v{*} var1-var4;
array x{*} x1-x4;
do i=1 to dim(v);
if v{i} not in x then x{i}=v{i};
else v{i}=.;
end;
keep var1-var4;
run;
Yes- you are right. it is about the 3rd observation
The dataset I want:
Var1 var2 var3 var4
1 2 3 4
0 . 4 9
1 . . 20
0 1 2 .
1 0 2 .
5 9 . .
3 . . .
data have;
input Var1 var2 var3 var4;
cards;
1 2 3 4
0 0 4 9
1 1 1 20
0 1 2 2
1 0 2 1
5 9 9 9
3 3 3 3
;
data want;
set have;
array v{*} var1-var4;
array x{*} x1-x4;
do i=1 to dim(v);
if v{i} not in x then x{i}=v{i};
else v{i}=.;
end;
keep var1-var4;
run;
Thanks very much.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.