SAS中如何填充字符字段?
我正在从包含 VARCHAR(5) 键字段的数据库创建 SAS 数据集。
此字段包括一些使用全部 5 个字符的条目和一些使用更少字符的条目。
当我导入此数据时,我更愿意填充所有较短的条目以使用所有五个字符。对于此示例,我想在左侧填充 0
(字符零)。因此,114
将变为 00114
,ABCD
将变为 0ABCD
,EA222
将变为保持原样。
我已经尝试使用简单的数据语句来执行此操作,但当然以下内容不起作用:
data test;
set databaseinput;
format key $5.;
run;
我尝试使用用户定义的信息来执行此操作,但我认为不可能在字符字段上正确指定范围,根据此 SAS 知识库答案。另外,我相当确定 proc 格式不会让我根据传入变量动态定义结果。
我确信这里有一个明显的解决方案,但我只是错过了它。
I am creating a SAS dataset from a database that includes a VARCHAR(5) key field.
This field includes some entries that use all 5 characters and some that use fewer.
When I import this data, I would prefer to pad all the shorter entries out to use all five characters. For this example, I want to pad on the left with 0
, the character zero. So, 114
would become 00114
, ABCD
would become 0ABCD
, and EA222
would stay as it is.
I've attempted this with a simple data statement, but of course the following does not work:
data test;
set databaseinput;
format key $5.;
run;
I've tried to do this with a user-defined informat, but I don't think it's possible to specify the ranges correctly on character fields, per this SAS KB answer. Plus, I'm fairly sure proc format won't let me define the result dynamically in terms of the incoming variable.
I'm sure there's an obvious solution here, but I'm just missing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个替代方案:
Here is an alternative:
我确信有人会有更优雅的解决方案,但下面的代码可以工作。本质上,它是用五个前导零填充变量,然后反转该文本字符串的顺序,使零位于右侧,然后再次反转该文本字符串并将大小限制为五个字符,按原始顺序但向左填充带零。
I'm sure someone will have a more elegant solution, but the following code works. Essentially it is padding the variable with five leading zeros, then reversing the order of this text string so that the zeros are to the right, then reversing this text string again and limiting the size to five characters, in the original order but left-padded with zeros.
这对我有用。
它的工作原理是计算现有字符串的长度,然后创建一个长度为 4 的 cat 字符串,然后将 cat 值和原始字符串附加在一起。
请注意,如果原始字符串长度为 5,则会出现问题。
另外 - 如果输入字符串有 $5,它将不起作用。格式化就可以了。
Here's what worked for me.
It works by counting the length of the existing string, and then creating a cat string of length 4 - that, and then appending the cat value and the original string together.
Notice that it screws up if the original string is length 5.
Also - it won't work if the input string has a $5. format on it.
我使用它,但仅适用于数值:S。尝试在输入中使用其他格式
I use this, but only works with numeric values :S. Try with another formats in the INPUT