问题描述
我正在尝试完成以下操作.
I am trying to complete the following.
变量字母具有三个值(a,b,c).我想创建一个变量Letter_2,其值对应于Letter的值,即(1、2、3).
Variable Letter has three values (a, b, c). I would like to create a variable Letter_2 with values corresponding to the values of Letter, namely (1, 2, 3).
我知道我可以使用三个IF Then语句来做到这一点.
I know I can do this using three IF Then statements.
if Letter='a' then Letter_2='1';
if Letter='b' then Letter_2='2';
if Letter='c' then Letter_2='3';
假设我有15个变量Letter值和15个对应的替换值.有没有一种方法可以有效地做到这一点,而无需键入相同的If If语句15次?
Suppose I have 15 values for the variable Letter, and 15 corresponding values for the replacement. Is there a way to do it efficiently without typing the same If Then statement 15 times?
我是SAS新手.任何线索将不胜感激.
I am new to SAS. Any clue will be appreciated.
丽莎
推荐答案
看起来像是FORMAT的应用程序.
Looks like an application for a FORMAT.
首先定义格式.
proc format ;
value $lookup 'a'='1' 'b'='2' 'c'='3' ;
run;
然后使用它重新编码您的变量.
Then use it to re-code your variable.
data want;
set have;
letter2 = put(letter,$lookup.);
run;
或者也许您可以使用两个临时数组和 WHICHC()
函数?
Or perhaps you could use two temporary arrays and the WHICHC()
function?
data have;
input letter $10. ;
cards;
car
apple
box
;;;;
data want ;
set have ;
array from (3) $10 _temporary_ ('apple','box','car');
array to (3) $10 _temporary_ ('first','second','third');
if whichc(letter,of from(*)) then
letter_2 = to(whichc(letter,of from(*)))
;
run;
这篇关于SAS如何有效地创建具有相应值的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!