BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

I am trying to flag the observations that contain the same value anywhere along the row. I can return all the observations that have one specific value somewhere along the row, but not what I am looking for. Can someone help please. 

 

What I want as output is:

 

ID      Time1   Time1    Time1      Flag

1234 Current Current                   1
5678 Current Current3 Current    0
3456 Corrent Past Current           0
2345 Current                                1    

 

 

 

DATA have;
INPUT ID Time1 $ 6-13 Time2 $14-22 Time3 $23-31;
CARDS; 
1234 Current 		  Current
5678 Current Current3 Current
3456 Corrent Past  	  Current
2345 Current 
;
RUN;

DATA want1;
SET have;
ARRAY x{*} time1-time3;
flag='Current' in x;
RUN;

 

 

 

 

2 REPLIES 2
Patrick
Opal | Level 21

Something like below should do the job.

data have;
  infile datalines truncover;
  input ID Time1 $ 6-13 Time2 $14-22 Time3 $23-31;
  datalines;
1234 Current          Current
5678 Current Current3 Current
3456 Corrent Past     Current
9876
2345 Current 
;

data want1(drop=_:);
  set have;
  array x{*} $ time1-time3;

  flag=0;
  do _i=1 to dim(x);
    if not missing(x[_i]) then
      do;
        if missing(_xi) then 
          do;
            _xi=_i;
            flag=1;
          end;
        else 
        if x[_xi] ne x[_i] then
          do;
            flag=0;
            leave;
          end;
      end;
  end;
run;
Ksharp
Super User
DATA have;
INPUT ID Time1 $ 6-13 Time2 $14-22 Time3 $23-31;
CARDS; 
1234 Current 		  Current
5678 Current Current3 Current
3456 Corrent Past  	  Current
2345 Current 
;
RUN;

DATA want1;
if _n_=1 then do;
 length k $ 100;
 declare hash h();
 h.definekey('k');
 h.definedone();
end;
SET have;
ARRAY x{*} $ time1-time3;
do i=1 to dim(x);
 if not missing(x{i}) then do;k=x{i};h.ref();end;
end;
flag=(h.num_items=1);
h.clear();
drop i k;
RUN;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 384 views
  • 0 likes
  • 3 in conversation