在SAS enterprise guide中将一行拆分为多行
当行上的值类似于1-5时,我需要帮助将一行拆分为多行。原因是我需要数到1-5才能变成5,而不是像数到一行时那样是1 我有一个ID,它的值和它所属的位置 例如:在SAS enterprise guide中将一行拆分为多行,sas,enterprise-guide,Sas,Enterprise Guide,当行上的值类似于1-5时,我需要帮助将一行拆分为多行。原因是我需要数到1-5才能变成5,而不是像数到一行时那样是1 我有一个ID,它的值和它所属的位置 例如: ID Value Page 1 1-5 2 我想要的输出如下: ID Value Page 1 1 2 1 2 2 1 3 2 1 4 2 1 5 2 我试过使用IF语句 IF bioVerdi='1-5' THEN DO;
ID Value Page
1 1-5 2
我想要的输出如下:
ID Value Page
1 1 2
1 2 2
1 3 2
1 4 2
1 5 2
我试过使用IF语句
IF bioVerdi='1-5' THEN
DO;
..
END;
所以我不知道我应该在两者之间做些什么;结束;。有什么线索可以帮我吗 您需要在范围内的值上循环,并输出值。
OUTPUT
语句使数据步骤向输出数据集写入记录
data want;
set have;
if bioVerdi = '1-5' then do;
do value=1 to 5;
output;
end;
end;
下面是另一个解决方案,它不局限于示例中给出的实际值“1-5”,但适用于格式为“1-6”、“1-7”、“1-100”等的任何值
*this is the data you gave ;
data have ;
ID = 1 ;
value = '1-5';
page = 2;
run;
data want ;
set have ;
min = scan( value, 1, '-' ) ; * get the 1st word, delimited by a dash ;
max = scan( value, 2, '-' ) ; * get the 2nd word, delimited by a dash ;
/*loop through the values from min to max, and assign each value as the loop iterates to a new column 'NEWVALUE.' Each time the loop iterates through the next value, output a new line */
do newvalue = min to max ;
output ;
end;
/*drop the old variable 'value' so we can rename the newvalue to it in the next step*/
drop value min max;
/*newvalue was a temporary name, so renaming here to keep the original naming structure*/
rename newvalue = value ;
run;
scan函数不会返回文本变量而不是数字变量吗?@Reeza-是的,scan函数会创建变量MIN和MAX,这是存储MIN='1'和MAX='2'的字符变量。但是,SAS会自动将“看起来”像数字的字符索引转换为do循环中的数字——因此
do newvalue='1'到'5'
被处理为do newvalue=1到5
,并且newvalue保留数字索引。这不会在消息中留下这样的日志吗?通常认为隐式转换不正确:)是的,它会留下:“注意:字符值已转换为数值…”。如果有麻烦,您可以轻松地将scan函数与输入函数包装在一起,将其转换,并从一开始就将其存储为数字:min=input(scan(value,1'-'),8.)代码>这是一个小细节,所以为了清晰起见,我在解决方案中留下了它。啊,是的。这可能是一个更好的解决方案,因为它是可伸缩的。唯一的问题是,我删除了所有没有以这种方式写入的值“1-5”。我得到的值也是5英尺。