SAS使用SQLDF转换为R转换

发布于 2025-01-31 08:28:09 字数 783 浏览 1 评论 0原文

感谢您使用SQLDF将以下SAS代码转换为R的某人。我正在努力更改数据类型并在一个步骤中应用子字符串功能。

同样,在第二步中,我不确定如何在另一个可以在稍后阶段使用的新字段中保存字段值。

 proc sql;
create table noodle as
select          MATRL_NBR,
             put(PLANT, z4.) as PLANT_NBR,
             PLANT_DESC,
             MFG_DT,
             input(substr(FISC_PD, 5, 4), best32.) as FISC_YR,
             input(substr(FISC_PD, 1, 3), best32.) as FISC_PD,
             input(substr(FISC_WK, 1, 3), best32.) as FISC_WK,
             PLANNED_ZNL,
             ACTUAL_ZNL,
             CRIT_SKU,
             IP_BRAND,
             IP_BU,
             CATG,
             MATRL_TYPE
from comb
;
quit;


proc sql noprint;
select          max(FISC_YR), max(FISC_WK)
into       :CURRENT_FISC_YR, :CURRENT_FISC_WK
from       noodle;
quit;

Appreciate if someone of you help me to convert below SAS code to R using SQLDF. I'm struggling to change the data type and apply sub string function in one single step.

Also In second step I'm not certain how to save the value of field in another new field which can be used in later stage.

 proc sql;
create table noodle as
select          MATRL_NBR,
             put(PLANT, z4.) as PLANT_NBR,
             PLANT_DESC,
             MFG_DT,
             input(substr(FISC_PD, 5, 4), best32.) as FISC_YR,
             input(substr(FISC_PD, 1, 3), best32.) as FISC_PD,
             input(substr(FISC_WK, 1, 3), best32.) as FISC_WK,
             PLANNED_ZNL,
             ACTUAL_ZNL,
             CRIT_SKU,
             IP_BRAND,
             IP_BU,
             CATG,
             MATRL_TYPE
from comb
;
quit;


proc sql noprint;
select          max(FISC_YR), max(FISC_WK)
into       :CURRENT_FISC_YR, :CURRENT_FISC_WK
from       noodle;
quit;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2025-02-07 08:28:09

我认为以下将实现您想要的东西:

library(sdldf)

noodle <- sqldf("select MATRL_NBR
              ,rightstr('0000'||cast(PLANT as varchar(4)),4) as PLANT_NBR 
              ,PLANT_DESC
              ,MFG_DT
              ,cast(substr(FISC_PD,5,4) as int) as FISC_YR
              ,cast(substr(FISC_PD,1,3) as int) as FISC_PD
              ,cast(substr(FISC_WK,1,3) as int) as FISC_WK
              ,PLANNED_ZNL
              ,ACTUAL_ZNL
              ,CRIT_SKU
              ,IP_BRAND
              ,IP_BU
              ,CATG
              ,MATRL_TYPE
      
      from comb")

CURRENT_FISC_YR <- max(noodle$FISC_YR)
CURRENT_FISC_WK <- max(noodle$FISC_WK)

您可能需要将用作double作为float而不是作为中的作为int cast()功能取决于您需要的数字数据类型,fisc_yr,fisc_pd和fisc_wk。

I think the following will achieve what you want:

library(sdldf)

noodle <- sqldf("select MATRL_NBR
              ,rightstr('0000'||cast(PLANT as varchar(4)),4) as PLANT_NBR 
              ,PLANT_DESC
              ,MFG_DT
              ,cast(substr(FISC_PD,5,4) as int) as FISC_YR
              ,cast(substr(FISC_PD,1,3) as int) as FISC_PD
              ,cast(substr(FISC_WK,1,3) as int) as FISC_WK
              ,PLANNED_ZNL
              ,ACTUAL_ZNL
              ,CRIT_SKU
              ,IP_BRAND
              ,IP_BU
              ,CATG
              ,MATRL_TYPE
      
      from comb")

CURRENT_FISC_YR <- max(noodle$FISC_YR)
CURRENT_FISC_WK <- max(noodle$FISC_WK)

You might want to use as double or as float instead of as int in the cast() functions depending on the numeric data type you require for FISC_YR, FISC_PD and FISC_WK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文