SAS中如何填充字符字段?

发布于 2024-12-14 09:01:00 字数 595 浏览 0 评论 0原文

我正在从包含 VARCHAR(5) 键字段的数据库创建 SAS 数据集。

此字段包括一些使用全部 5 个字符的条目和一些使用更少字符的条目。

当我导入此数据时,我更愿意填充所有较短的条目以使用所有五个字符。对于此示例,我想在左侧填充 0(字符零)。因此,114 将变为 00114ABCD 将变为 0ABCDEA222 将变为保持原样。

我已经尝试使用简单的数据语句来执行此操作,但当然以下内容不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南街女流氓 2024-12-21 09:01:00

这是一个替代方案:

data padded_data_dsn; length key $5;
    drop raw_data;
    set raw_data_dsn(rename=(key=raw_data));
    key = translate(right(raw_data),'0',' ');
run;

Here is an alternative:

data padded_data_dsn; length key $5;
    drop raw_data;
    set raw_data_dsn(rename=(key=raw_data));
    key = translate(right(raw_data),'0',' ');
run;
凹づ凸ル 2024-12-21 09:01:00
Data raw_data_dsn;
format key $5.;
key = '4'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
key = 'A114'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
key = 'A1140'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
run;
Data raw_data_dsn;
format key $5.;
key = '4'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
key = 'A114'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
key = 'A1140'; key1 = CATT(REPEAT('0',5-length(key)),key);output;
run;
携君以终年 2024-12-21 09:01:00

我确信有人会有更优雅的解决方案,但下面的代码可以工作。本质上,它是用五个前导零填充变量,然后反转该文本字符串的顺序,使零位于右侧,然后再次反转该文本字符串并将大小限制为五个字符,按原始顺序但向左填充带零。

data raw_data_dsn;
   format key $varying5.;
   key = '114'; output;
   key = 'ABCD'; output;
   key = 'EA222'; output;
run;

data padded_data_dsn;
   format key $5.;
   drop raw_data;
   set raw_data_dsn(rename=(key=raw_data));
   key = put(put('00000' || raw_data ,$revers10.),$revers5.);
run;

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.

data raw_data_dsn;
   format key $varying5.;
   key = '114'; output;
   key = 'ABCD'; output;
   key = 'EA222'; output;
run;

data padded_data_dsn;
   format key $5.;
   drop raw_data;
   set raw_data_dsn(rename=(key=raw_data));
   key = put(put('00000' || raw_data ,$revers10.),$revers5.);
run;
2024-12-21 09:01:00

这对我有用。

data b (keep = str2);
    format str2 $5. ;
    set a;
    catlength = 4 - length(str); 
    cat = repeat('0', catlength);
    str2 = catt(cat, str); 
run;

它的工作原理是计算现有字符串的长度,然后创建一个长度为 4 的 cat 字符串,然后将 cat 值和原始字符串附加在一起。

请注意,如果原始字符串长度为 5,则会出现问题。
另外 - 如果输入字符串有 $5,它将不起作用。格式化就可以了。

data a; /*input dataset*/
    input str $;
    datalines;
    a
    aa
    aaa
    aaaa
    aaaaa
    ;
run;

data b (keep = str2);
    format str2 $5. ;
    set a;
    catlength = 4 - length(str); 
    cat = repeat('0', catlength);
    str2 = catt(cat, str); 
run;

input:
a
aa
aaa
aaaa
aaaaa

output:
0000a   
000aa   
00aaa   
0aaaa   
0aaaa   

Here's what worked for me.

data b (keep = str2);
    format str2 $5. ;
    set a;
    catlength = 4 - length(str); 
    cat = repeat('0', catlength);
    str2 = catt(cat, str); 
run;

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.

data a; /*input dataset*/
    input str $;
    datalines;
    a
    aa
    aaa
    aaaa
    aaaaa
    ;
run;

data b (keep = str2);
    format str2 $5. ;
    set a;
    catlength = 4 - length(str); 
    cat = repeat('0', catlength);
    str2 = catt(cat, str); 
run;

input:
a
aa
aaa
aaaa
aaaaa

output:
0000a   
000aa   
00aaa   
0aaaa   
0aaaa   
傲世九天 2024-12-21 09:01:00

我使用它,但仅适用于数值:S。尝试在输入中使用其他格式

data work.prueba;
    format xx $5.;
    xx='1234';
    vv=PUT(INPUT(xx,best5.),z5.);
run;

I use this, but only works with numeric values :S. Try with another formats in the INPUT

data work.prueba;
    format xx $5.;
    xx='1234';
    vv=PUT(INPUT(xx,best5.),z5.);
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文