BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BayzidurRahman
Obsidian | Level 7

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          .           .           .

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

4 REPLIES 4
JosvanderVelden
SAS Super FREQ
The logic you want to implement is not clear since the second observation doesn't follow the guidelines you describe. In that observation you have two '1's! If I understand your guidelines the second observation should be 1 . . 20. Can you explain why that is not the case?
BayzidurRahman
Obsidian | Level 7

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          .           .           .

Ksharp
Super User
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;
BayzidurRahman
Obsidian | Level 7

Thanks very much.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 824 views
  • 1 like
  • 3 in conversation