BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
angeliquec
Quartz | Level 8

Hi,

 

I have a data set with missing data which I'd like to fill in with the next nonmissing value across the array. Or it's like moving values across the array. 

 

data have;

input ID num1 num2 num3 num4;

cards;

1 1 2 . 4

2 1 . . 4

3 1. 3 4

;

run;

 

data want;

input ID num1 num2 num3 num4;

cards;

1 1 2 4 .

2 1 4 . .

3 1 3 4 .

;

run;

 

 

Can I produce the want data without transposing?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID num1 num2 num3 num4;
cards;
1 1 2 . 4
2 1 . . 4
3 1 . 3 4
;
run;
data want;
 set have;
 array x{4} _num1-_num4;
 array y{4} num1-num4;

 n=0;
 do i=1 to dim(y);
  if not missing(y{i}) then do;
   n+1;x{n}=y{i};
  end;
 end;
drop i n num:;
run;

View solution in original post

5 REPLIES 5
rogerjdeangelis
Barite | Level 11
* reverse sort of an aaray;

data have;
input ID num1 num2 num3 num4;
cards;
1 1 2 . 4
2 1 . . 4
3 1 . 3 4
;
run;

Up to 40 obs WORK.HAVE total obs=3

Obs    ID    NUM1    NUM2    NUM3    NUM4

 1      1      1       2       .       4
 2      2      1       .       .       4
 3      3      1       .       3       4


WANT

Up to 40 obs WORK.WANT total obs=3

Obs    ID    NUM1    NUM2    NUM3    NUM4

 1      1      4       2       1       .
 2      2      4       1       .       .
 3      3      4       3       1       .


SOLUTION

data want;
    set have;
    array arr[*] num1-num4;
    array rev[*] num4-num1;
    call sortn(of rev[*]);
  run;

rogerjdeangelis
Barite | Level 11

CORREC

data want;
    set have;
    array arr[*] num1-num4;
    do i=1 to 4;
      if arr[i]=. then arr[i]=constant('BIG');
    end;
    call sortn(of arr[*]);
    do i=1 to 4;
      if arr[i]=constant('BIG') then arr[i]=.;
    end;
    drop i;
  run;


Up to 40 obs WORK.WANT total obs=3

Obs    ID    NUM1    NUM2    NUM3    NUM4

 1      1      1       2       4       .
 2      2      1       4       .       .
 3      3      1       3       4       .

TION

 

 

PGStats
Opal | Level 21

No tricks; just simple programming:

 

data want;
set have;
array num num:;
j = 0;
do i = 1 to dim(num);
    if not missing(num{i}) then do;
        j + 1;
        num{j} = num{i};
        end;
    end;
do i = j + 1 to dim(num);
    call missing(num{i});
    end;
drop i j;
run;
PG
KachiM
Rhodochrosite | Level 12

Another way:

Look for a missing value, note the position(i). Then move forward to find the next non-missing value to replace the i-th value while marking the j-th value as missing. Skip the check. Repeat the above step for the next i-th value.

 

data need;
   set have;
      array num num: ;
      do i = 1 to dim(num) - 1;
         if missing(num[i]) then 
         do j = i + 1  to dim(num);
            if not missing(num[j]) then do; 
               num[i] = num[j]; num[j] = .; leave; 
            end;
         end;
      end;
drop i j;
run;
Ksharp
Super User
data have;
input ID num1 num2 num3 num4;
cards;
1 1 2 . 4
2 1 . . 4
3 1 . 3 4
;
run;
data want;
 set have;
 array x{4} _num1-_num4;
 array y{4} num1-num4;

 n=0;
 do i=1 to dim(y);
  if not missing(y{i}) then do;
   n+1;x{n}=y{i};
  end;
 end;
drop i n num:;
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 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2049 views
  • 1 like
  • 5 in conversation