在 SAS Enterprise Guide 中将一行拆分为多行
Split a row into multiple rows in 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;
..
END;
所以我不知道应该在 DO 之间放什么;和结束;。有什么线索可以帮助我吗?
您需要遍历范围内的值和 OUTPUT
值。 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;
当某行的值大约为 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;
..
END;
所以我不知道应该在 DO 之间放什么;和结束;。有什么线索可以帮助我吗?
您需要遍历范围内的值和 OUTPUT
值。 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;