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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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