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?
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.
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 -
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
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.