SAS使用proc sql插入值

SAS使用proc sql插入值,sas,proc-sql,Sas,Proc Sql,所以我有一个很有趣的问题。我试图以特定的格式和样式插入当前日期,但由于某些原因,它似乎失败了。我知道这不是一个格式问题。。。但我不知道如何修复它。数据步骤解决方案也受到欢迎。。。以下是有效的方法 proc sql; create table work.test (test_Id char(50), test_Name char(50), cur_Mo char(1), cur_Qtr char(1), entered_Date char(8)); insert into work.test v

所以我有一个很有趣的问题。我试图以特定的格式和样式插入当前日期,但由于某些原因,它似乎失败了。我知道这不是一个格式问题。。。但我不知道如何修复它。数据步骤解决方案也受到欢迎。。。以下是有效的方法

proc sql;
create table work.test
(test_Id char(50), test_Name char(50), cur_Mo char(1), cur_Qtr char(1), entered_Date char(8));
insert into work.test 
values('201703','2017 Mar','0','0','24APR17')
values('201704','2017 Apr','0','0','24APR17')
values('201706','2017 Jun','1','0','23JUN17');
quit;
以下是不需要的:

proc sql;
    insert into work.test 
    values(catx('',put(year(today()),4.),case when month(today())< 10 then catx('','0',put(month(today()),2.)) else put(month(today()),2.)end) ,catx(' ',[put(year(today()),4.),put(today(),monname3.))],'1','0',put(today(),date7.));
quit;
proc-sql;
插入到work.test中
值(catx(“”,put(year(today()),4.),当月份(today())小于10时的大小写,然后catx(“”,'0',put(month(today()),2.)其他put(month(today()),2.)end),catx(“”,'put(year(today()),4.),put(today(),monname3.))],'1','0',put(today(),date7.);
退出
可以使用%SYSFUNC()宏函数调用宏代码中的大多数其他SAS函数。因此,要以DATE7格式生成今天的日期,您可以使用:

insert into work.test (date)
  values("%sysfunc(date(),date7)")
;

我可能会使用一个数据步骤来创建一个要插入的数据集,然后插入该数据集

您可以在SAS中使用
insert-into(…)select(…)from(…)
语法,数据步骤更加灵活,允许您定义列

例如:

proc sql;
  create table class like sashelp.class;
quit;

proc sql;
   insert into class
     select * from sashelp.class;
quit;
或者,您只能指定某些变量:

proc sql;
    insert into class (name, age)
      select name, age from sashelp.class;
quit;



data to_insert;
  name= 'Wilma';
  sex = 'F';
  age = 29;
  height = 61.2;
  weight = 95.3;
run;

proc sql;
  insert into class
    select * from to_insert;
quit;

只需确保您明确列出了要插入/选择的变量,或者您的顺序完全正确(如果您像上面一样使用
*
,它会按位置匹配)。

您不能将函数放入VALUES子句中。只是实际值。我想是这样的,还有其他方法吗?谢谢你的建议,如果有一个日期函数作为变量之一呢?我也要试试这个!谢谢在数据步骤中,这不是问题;date函数应该在数据步骤中解析(即,不要插入文本“today()”,但是
age=today()-birthdate;
应该可以,因为它在数据步骤中进行计算)。如果您使用
选择
从表中插入数据,而不是使用
的常量,那么您可以使用函数。我有一个跟进-我如何组合月份和日期,使其如下:201701201702等等?SAS有许多日期格式。YYMMN6将生成YYYYMM格式的日期字符串。在我的情况下,我的解决方案是在Insert语句中使用它:
值(“%sysfunc(date(),YYMMN6.)”,“%sysfunc(date(),yymon7.),“1”,“0”,““%sysfunc(date(),date7.)”目前,这符合我当前问题的要求。谢谢