简体   繁体   English

如何在 SAS 中的 proc sql 查询中的 select 列名称“startwith”

[英]How to select column name “startwith” in proc sql query in SAS

I am looking a way to select all columns name that "startwith" a specific character.我正在寻找一种方法来 select 所有以特定字符“开头”的列名称。 My data contains the same column name multiple time with a digit number at the end and I want the code to always select all the columns regardless the last digit numbers.我的数据多次包含相同的列名,最后有一个数字,我希望代码始终为 select 所有列,而不考虑最后一个数字。

For example, if I have 3 kinds of apple in my column names, the dataset will contains the column: "apple_1", "apple_2" and "apple_3".例如,如果我的列名中有 3 种苹果,则数据集将包含列:“apple_1”、“apple_2”和“apple_3”。 Therefore, I want to select all columns that startwith "apple_" in a proc sql statement.因此,我想 select 在 proc sql 语句中以“apple_”开头的所有列。

Thanks you谢谢

In regular SAS code you can use : as a wildcard to create a variable list.在常规 SAS 代码中,您可以使用:作为通配符来创建变量列表。 You normally cannot use variable lists in SQL code, but you can use them in dataset options.您通常不能在 SQL 代码中使用变量列表,但您可以在数据集选项中使用它们。

proc sql ;
create table want as 
select *
from mydata(keep= id apple_: )
;
quit;

Use like : like使用:

proc sql;
    select t.*
    from t
    where col like 'apple%';

If you want the _ character as well, you need to use the ESCAPE clause, because _ is a wildcard character for LIKE :如果您还想要_字符,则需要使用ESCAPE子句,因为_LIKE的通配符:

proc sql;
    select t.*
    from t
    where col like 'apple$_%' escape '$';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM