使用循环在SPSS中创建指标变量(Create indicator variables in SPSS using loops)
我试图使用循环在SPSS中创建一些指标变量。
我想创建17个新变量ABA(1-17),如果BA(1-17)等于1,则取值为1.我尝试的是:
VECTOR ABA(17). LOOP #i = 1 to 17. IF(BA(#i)=1) ABA(#i) = 1. END LOOP. EXECUTE.
遗憾的是,这只会创建缺少值的变量。 上面的代码是否只需要一个小的调整,或者它们是一种更有效的方法来完成创建变量?
I am trying to create some indicator variables in SPSS using a loop.
I want to create 17 new variables ABA(1-17) that take a value of 1 if BA(1-17) equals 1. I was trying something like:
VECTOR ABA(17). LOOP #i = 1 to 17. IF(BA(#i)=1) ABA(#i) = 1. END LOOP. EXECUTE.
This, unfortunately, just creates variables with missing values. Does the above code just need a small tweak or is their a more efficient way to accomplish creating the variables?
原文:https://stackoverflow.com/questions/40533385
最满意答案
您不需要
ALTER
语句,但可以在获取类似数据时将其转换为datetime
时间select cast(order_date as datetime) as new_date from table1;
但是你不应该把日期时间数据存储为字符串或
varchar
。 它应该只存储在datetime
数据类型列中。 现在,由于您已经有该列中的数据,ALTER
看起来有点误导。您可以备份当前的数据。 创建一个具有
order_date datetime
相同结构的中间表。 然后执行一个insert into .. select from
下面的insert into .. select from
。 然后可能将该表重命名为原始表(在此之前删除现有表);insert into intermediary_table select col1,col2, cast(order_date as datetime), col3 from table1;
You don't need a
ALTER
statement but can convert that todatetime
while fetching the data likeselect cast(order_date as datetime) as new_date from table1;
But you should never store datetime data as string or
varchar
. It should be stored indatetime
datatype column only. Now, since you already have data in that column;ALTER
looks bit misleading.You can take a backup of your current data. Create a intermediary table with same structure having
order_date datetime
. Then perform ainsert into .. select from
like below. Then probably rename that table to original table (before that drop the existing table);insert into intermediary_table select col1,col2, cast(order_date as datetime), col3 from table1;
相关问答
更多-
您不需要ALTER语句,但可以在获取类似数据时将其转换为datetime时间 select cast(order_date as datetime) as new_date from table1; 但是你不应该把日期时间数据存储为字符串或varchar 。 它应该只存储在datetime数据类型列中。 现在,由于您已经有该列中的数据, ALTER看起来有点误导。 您可以备份当前的数据。 创建一个具有order_date datetime相同结构的中间表。 然后执行一个insert into .. sel ...
-
Varchar值为日期格式(Varchar values to date format)[2023-08-01]
嗯。 。 。 你真的想把日期存储为日期。 在您的情况下,您可以使用try_convert()并继续测试,直到它工作: select coalesce(try_convert(date, shipdate), try_convert(date, shipdate, 1), try_convert(date, shipdate, 101) ) as presumed_date Hmmmm . . . You real ... -
Varchar到日期和时间(Varchar to Date and Time)[2024-01-12]
我不建议在字符串中保存日期,您可以将数据类型保存为Date或DateTime数据类型, Q1的答案是 SELECT SUBSTRING(MyColumn, 5,2) + RIGHT(MyColumn, 2) + LEFT(MyColumn, 4) 但请记住它的作品只有当你的YYYYMMDD格式的刺痛,如果你改变格式化然后错误的答案将来或可能会得到错误和Q2的答案是 SELECT LEFT(MyColumn, 2) + ':' + RIGHT(MyColumn, 2) i will not recomme ... -
使用STR_TO_DATE以正确的格式获取数据 UPDATE `date` SET `date` = STR_TO_DATE(`date`,'%a %b %H:%i:%s +0000 %Y') 然后改变你的表格: ALTER TABLE mytable ALTER COLUMN `date` DATETIME NOT NULL; Use STR_TO_DATE to get the data in the right format UPDATE `date` SET `date` = STR_TO_DA ...
-
像eggyal说的, SELECT STR_TO_DATE('Monday 16th September 2013','%W %D %M %Y') 完成您指定的日期布局的工作。 这也适用于包含日期的列。 例如,如果您在列中包含这些日期: what_day Monday 16th September 2013 Tuesday 17th September 2013 Thursday 19th September 2013 Friday 20th September 2013 Saturday 21st ...
-
在这种情况下,您可以使用SUBSTR函数来剪切最后4个字符。 SELECT * FROM yourtable where SUBSTR(flight_date, -4) = '2016' 虽然,建议将日期值存储在适当的类型列中,而不是varchar中。 In this case, you can use SUBSTR function for just cut last 4 character. SELECT * FROM yourtable where SUBSTR(flight_date, -4) ...
-
而且您无法按照自己的方式更改列。 您必须创建临时日期列 ALTER TABLE yourTable add tempDate date not null; 然后使用格式化的日期更新它: UPDATE yourTable SET tempDate = to_date( yourVarcharDateColumn, 'mm/dd/yy' ); 然后删除列并重新创建它 ALTER TABLE yourTable DROP COLUMN yourVarcharDateColumn; ALTER TABLE yo ...
-
我认为你的问题有一个更简单的解决方案。 这是一个命题: SELECT CONVERT(datetime, any_datetime_format) unified_datetime from ( SELECT '2/8/2017 8:06:56 AM' any_datetime_format UNION SELECT '2/2/2017 2:42:09 PM' any_datetime_format UNION SELECT '1/3/2017 9:10:20 AM' any_dat ...
-
你在找这样的东西吗? SELECT DocNum, DateDue FROM prod_po_list WHERE STR_TO_DATE(DateDue, '%m/%d/%Y') BETWEEN DATE_SUB(CURDATE(), INTERVAL 365 DAY) AND CURDATE() Are you looking for something like this? SELECT DocNum, DateDue FROM prod_po_list ...
-
你只想在第十个字符之后插入一些东西。 使用substr函数 : UPDATE BigTable SET AField = substr(AField, 1, 10) || ',' || substr(AField, 11) You simply want to insert something after the tenth character. Use the substr function for this: UPDATE BigTable SET AField = substr(AField, 1, ...