SAS 数据集字段类型 num 到 char 当前格式
我目前有一个数据集,其中包含我需要的变量以及所需的格式。
现在,我在循环中使用 getvarc()
函数(以及其他函数)来将这些变量写入文件。
当然,出现的第一个问题是有些变量不是 char 类型而是 num 类型。我可以使用 getvarn() 来检索这些内容,但是这样我真正需要的格式就会被浪费。
例如 num 类型的日期:18750。格式 = yymmdd10。因此,看起来像 2011-05-03
对于该字段使用 value=getvarn()
,它将检索 18750
的值。如果我想输出(使用 PUT)这个值,它不会给我我想要的 2011-05-03 日期。
所以现在我正在寻找更好的方法来做到这一点。
我的第一个选择是使用 sashelp.vcolumn 数据集,检索该字段的格式,并在 put 语句中使用它以正确的格式输出。
我的第二个选择是转换包含此字段的数据集(实际上它涉及多个数据集),其中我将所有 num 类型字段转换为保持正确格式的 char 类型。
我应该选择哪个选项?
如果是第二种选择,是否可以以通用方式完成(正如我所说,这不仅仅是一个变量)?
编辑:
在 Cmjohn 的回答之后,我找到了解决我的问题的方法。不使用 sashelp.vcolumn,而是使用 varfmt 函数。 因此,对我在代码中所做的事情进行简短的解释:
data _null_
set dataset1;
file file1;
if somefield = x then do;
dsid = open(datasetfield, i);
rc = fetchobs (dsid);
varnum = varnum(dsid,anotherfield);
**varfmt = varfmt(dsid,anotherfield);
if vartype(dsid,anotherfield) = 'N' then do;**
value = getvarn(dsid,varnum);
**value_formatted = putn(value,varfmt);**
**end;
else do;**
value = getvarc(dsid,varnum);
**value_formatted = putc(value,varfmt);
end;**
put value_formatted;
end;
run;
这就是我现在正在做的事情(很快就从我的脑海中消失了)。解决方案的大胆部分是我在 Cmjohns 回复后想到的。所以我的第一个问题得到了回答:“怎么做?”。
但我补充的问题是:从长远来看什么是最有效的:保留此过程或使我的数据集仅使用 getvarc
即可读取所有数据,而无需类型检查和 getfmt()
?
I have currently a dataset which contains the variables I need together with the needed formats.
Now I am using the getvarc()
function (among others) in a loop to get those variables to write to a file.
The first problem occuring ofcourse, is that some variables are not char type but num type. I could use getvarn()
to retrieve those, but then the format goes to waste which I really need.
E.g. date in num type: 18750. Format = yymmdd10. Thus looking like 2011-05-03
Using the value=getvarn()
for this field, it would retrieve 18750
for value. If I then want to output (using PUT) this value, it would not give me the 2011-05-03
date as I want.
So now I am looking for a better way to do this.
My first option is to use the sashelp.vcolumn data set, to retrieve the format on this field, and use it in the put statement to output in the right format.
My second option is to convert the data set containing this field (in reality it is about multiple data sets), where I convert all num type fields to char types remaining the right format.
Which option should I go with?
And in case of the second option, can this be done in a generic way (as I said, this is not about one variable only) ?
EDIT:
After the answer of Cmjohn I got to find a way to fix my problem. Not using the sashelp.vcolumn but using the varfmt
function.
So a short explanation of what I am doing in my code:
data _null_
set dataset1;
file file1;
if somefield = x then do;
dsid = open(datasetfield, i);
rc = fetchobs (dsid);
varnum = varnum(dsid,anotherfield);
**varfmt = varfmt(dsid,anotherfield);
if vartype(dsid,anotherfield) = 'N' then do;**
value = getvarn(dsid,varnum);
**value_formatted = putn(value,varfmt);**
**end;
else do;**
value = getvarc(dsid,varnum);
**value_formatted = putc(value,varfmt);
end;**
put value_formatted;
end;
run;
So this is in general (quickly out of my head) what I am doing now. The bold part of the solution is what I came up with after Cmjohns response. So my first question is answered, 'How to do it?'.
But my added question: What would be most efficient in the long run: keep this process or make my data sets the way that all data can be read in by only using getvarc
without the need of the type-check and the getfmt()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 vvalue 函数,或 这个问题 将格式化值放入文件中。
I'd suggest that you use the vvalue function, or any of the other techniques provided in the answers to this question to put formatted values to a file.