英文:
Using char.sub command on multiple variables
问题
我必须选择多个变量中以特定数字开头的一组案例。
我正在使用以下代码:
CHAR.SUBSTR(variable1,1,x) ="y" | CHAR.SUBSTR(variable2,1,x) ="y" .............| CHAR.SUBSTR(variable40,1,x) ="y".
(x是字符数,y是我选择的字符),这些变量的名称相似,只有数字1到40不同。
这个方法有效,但问题是有40个变量,代码非常冗长。有没有更优雅的方式来编写它?比如 variable1 到 variable 40?
英文:
I have to select group of cases starting with specific numbers in multiple variables.
I am using this
CHAR.SUBSTR(variable1,1,x) ="y" | CHAR.SUBSTR(variable2,1,x) ="y" .............| CHAR.SUBSTR(variable40,1,x) ="y".
(x is number of character,y is characters I am choosing) the variables are named similar with just the number 1 to 40 being different
it works but problem is there are 40 variables and code is very length.
any elegant way to write it? like variable1 THRU variable 40?
答案1
得分: 1
你可以循环遍历变量,然后进行选择。就像这样:
do repeat vr=variable1 to variable40.
if CHAR.SUBSTR(vr,1,1)="y" keep_this=1.
end repeat.
select if keep_this=1.
运行循环后,如果任何变量以 "y" 开头,那么该行将在变量 keep_this
中标记为 1。现在你可以仅选择 keep_this=1
的案例。
英文:
You can loop through the variables and then select. Like this:
do repeat vr=variable1 to variable40.
if CHAR.SUBSTR(vr,1,1)="y" keep_this=1.
end repeat.
select if keep_this=1.
after running the loop, if any of the variables starts with "y" then the line will be marked with 1 in the variable keep_this
. Now you can select only cases where keep_this=1
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论