BookmarkSubscribeRSS Feed
soumri
Quartz | Level 8

Hello everybody;
I ask if there is a method in SAS, which allows to assign a missing value of a variable x in a Table A,

a value y  from a table B which contains common values and of course under some conditions, like this for exemple:

 

Table A:

 

ID              CM      NE       Rank         X

BV012       1         1         1              12.5

BV012       1         1         2              .

BV012       1         1         3              13.0

BV012       11       3         1              .

BV012       11       3         2              13.4

BV012       11       3         3              11.8

BV013       6         2         1              10.9

BV013       6         2         2              09.6

BV013       6         2         3              09.5

BV013       6         2         4              .

BV017       1         1         1              22.1

BV017       1         1         2             17.8

CM=mounth (1,..12)

NE=order activity (1,2 or 3)

Rank=rank (from 1 to 20)

 

TABLE B: ( common values) given by mounth (CM):

 

Mounth    NE           RANK            Y

1             1              1                  15.5

1             1              2                  18.3          

1             1              3                  17.2          

...            ...             ...                  ...

1             1              20                03.5

----------------------------------------------

1             2              1                  15.8

...            ...             ...                  ...

1             2              20                03.4

----------------------------------------------

...            ...             ...                  ...

1             3              20                07.3

----------------------------------------------

2             1              1                  22.3

...            ...             ...                  ... 

----------------------------------------------

...            ...             ...                  ... 

...            ...             ...                  ... 

----------------------------------------------

12           3              20                05.5

 

 

 

 

what i want:

if x from table A = "."  then x= y from table B for the same mounth=(1,2,...12), for the same NE=(1,2,3) and for the same Rank = (1,2,... 20).

I did it but it was a very long code. Is there a shorter method with proc sql for exemple.

thanks.

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The way to do this is to merge to the two datasets by the common variables, then use the coalesce function to take the first non missing:

proc sql;
  create table WANT as
  select  A.COMMON_VAR,
             COALESCE(A.VAR,B.VAR) as VAR
  from    FIRST A
  left join  SECOND B
  on       A.COMMON_VAR=B.COMMON_VAR;
quit;

This will take A.VAR if it is not null, otherwise B.VAR.

Ksharp
Super User

UPDATE . If table is big ,Try Hash Table.

 

data table_a;
input ID    $          CM      NE       Rank         X;
cards;
BV012       1         1         1              12.5
BV012       1         1         2              .
BV012       1         1         3              13.0
BV012       11       3         1              .
BV012       11       3         2              13.4
BV012       11       3         3              11.8
;
run;

data table_b;
input Mounth    NE           RANK            Y;
cards;
1             1              1                  15.5
1             1              2                  18.3          
1             1              3                  17.2 
;
run;
data want;
 update table_b(rename=(Mounth=CM Y=X)) table_a(in=ina);
 by  CM      NE       Rank ;
 if ina;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 2 replies
  • 1143 views
  • 0 likes
  • 3 in conversation