BookmarkSubscribeRSS Feed
rohit_prajapati
Calcite | Level 5

Hi ALL,

Have -

DATA TEST;
INPUT SUBJECT $ NUM1 NUM2 NUM3 NUM4 NUM5;
DATALINES;
A01 . 2 5 36 .
B01 65 25 54 . 3
C01 4 . . 4 5
D01 . . 5 4 5
E01 . . . 5 4
RUN;


Want -

SUBJECTNUM1NUM2NUM3NUM4NUM5SUM_NUM
A014325364343
B016525541473147
C01413134513
D01141454514
E01999549

The value of SUM_NUM has to be place where you can see ' . ' (dot)

Example - For subject A01 NUM1 has dot (.) which replace the value of SUM_NUM.

Kindly make sure that there are n number of NUM coloumn.

Thanks in Advance.

Regards,

Rohit

7 REPLIES 7
Amir
PROC Star

Hi Rohit,

Does the following do what you want:

data want;

  set test;

  sum_num=sum(of num1-num5);

  if nmiss(of num1-num5) then

  do;

    if nmiss(num1) then

      num1=sum_num;

    if nmiss(num2) then

      num2=sum_num;

    if nmiss(num3) then

      num3=sum_num;

    if nmiss(num4) then

      num4=sum_num;

    if nmiss(num5) then

      num5=sum_num;

  end;

run;

Regards,

Amir.

Keith
Obsidian | Level 7

data want;

set test;

array nums{*} NUM: ;

_sumnum=sum(of nums{*});

do _i=1 to dim(nums);

  if missing(nums{_i}) then nums{_i}=_sumnum;

end;

drop _: ;

run;

Linlin
Lapis Lazuli | Level 10

DATA TEST;

INPUT SUBJECT $ NUM1 NUM2 NUM3 NUM4 NUM5;

DATALINES;

A01 . 2 5 36 .

B01 65 25 54 . 3

C01 4 . . 4 5

D01 . . 5 4 5

E01 . . . 5 4

RUN;

%let n=5;

data want;

  set test;

    array _num num1-num&n;

  sum_num=sum(of num1-num&n);

   do over _num;

   if _num=. then _num=sum_num;

   end;

  run;

proc print;run;

   Obs    SUBJECT    NUM1    NUM2    NUM3    NUM4    NUM5    sum_num

          1       A01       43       2       5       36     43        43

          2       B01       65      25      54      147      3       147

          3       C01        4      13      13        4      5        13

          4       D01       14      14       5        4      5        14

          5       E01        9       9       9        5      4         9

Haikuo
Onyx | Level 15

data want;

  set test;

   array num num:;

       sum_number=sum(of num:);

       do over num;

         num=coalesce(num, sum_number);

        end;

run;

Haikuo

rohit_prajapati
Calcite | Level 5

wow great Hai.Kuo.

But How COALESCE function works ? Never seen this function.

Can u pls explain?

Rohit

Patrick
Opal | Level 21

COALESCE takes as documented in the SAS Online doc the first non missing value from a list.

rohit_prajapati
Calcite | Level 5

Wow thats great.

Thank you everyone.

i had already tried with Amir code but i wanted in shortest method but its wonderful.

Thanks everyone.

Regards,

Rohit

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 2808 views
  • 3 likes
  • 6 in conversation