BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12
PROC SQL;
     CREATE TABLE WORK.BASE AS
     SELECT *
         ,A.TGT_QTY*B.CNV_FCTR AS PLANNED_ZNL2
     FROM WORK.BASELINE AS A LEFT JOIN WORK.AT_UOM AS B
       ON A.MATRL_NBR = B.MATRL_NBR 
       AND A.BASE_UOM =B.BASE_UOM  
       AND B.ALTN_UOM = 'ZNL';
RUN;

DATA WORK.BASE (DROP=PLANT_DESC CNV_FCTR
     ALTN_UOM TGT_QTY BASE_UOM PLANNED_ZNL2);
     SET WORK.BASE;
     IF _ZNL = . THEN _ZNL = PLANNED_ZNL2;
;
RUN;

Any help to combine the below two steps into one?

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20
PROC SQL;
     CREATE TABLE WORK.BASE AS
     SELECT *
          , A.TGT_QTY*B.CNV_FCTR AS PLANNED_ZNL2
		  , case when _ZNL = . then Planned else . end as _ZNL
     FROM WORK.BASELINE AS A LEFT JOIN WORK.AT_UOM AS B
       ON A.MATRL_NBR = B.MATRL_NBR 
       AND A.BASE_UOM =B.BASE_UOM  
       AND B.ALTN_UOM = 'ZNL';
RUN;

Unstested.

Oligolas
Barite | Level 11

It's feasible but it's non-sense because the code becomes unreadable

PROC SQL;
     CREATE TABLE WORK.BASE(DROP=PLANT_DESC CNV_FCTR ALTN_UOM TGT_QTY BASE_UOM PLANNED_ZNL2 _ZNL_hlp) AS
     SELECT *
         ,A.TGT_QTY*B.CNV_FCTR AS PLANNED_ZNL2
         ,case 
            when _ZNL_hlp = . THEN PLANNED_ZNL2
            else _ZNL_hlp
         end as _ZNL
     FROM WORK.BASELINE AS A LEFT JOIN WORK.AT_UOM(rename=(_ZNL=_ZNL_hlp)) AS B
       ON A.MATRL_NBR = B.MATRL_NBR 
       AND A.BASE_UOM =B.BASE_UOM  
       AND B.ALTN_UOM = 'ZNL';
QUIT;

you need to rename the _ZNL variable because you can't add it twice to the dataset. (select *)

@David_Billa make sure you always terminate proc sql with QUIT;

________________________

- Cheers -

Kurt_Bremser
Super User

DO NOT use the asterisk like this. You have at least two variables in common between the two datasets, so SQL will throw a WARNING, and you may get undesired results. Always (as in ALWAYS) use a comprehensive list of variables in the SELECT when doing a JOIN.

In the opinion of senior SAS users (including me), SAS should not throw a WARNING here, but an ERROR, and refuse to accept such crap code. Like SQL in other systems does.

 

You can do the "drop" by not including the unwanted variables in the SELECT, and calculate _ZNL like this:

,coalesce(_znl,a.tgt_qty * b.cnv_rctr) as _znl

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

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
  • 3 replies
  • 908 views
  • 1 like
  • 4 in conversation