英文:
SAS Loop through data to add labels in the style of "1.9, 1.10, 1.11" etc
问题
我正在运行一些 SAS 代码,对一个按组排序并且每个组内有多个条目的表执行操作。我希望根据排序顺序为所有条目分配唯一的标识符。例如:
组 项目 标签
A Z 1.1
A Y 1.2
B Z 2.1
B Y 2.2
许多组具有超过10个项目,但我不希望计数仅在下一个整数值上翻转。我应该编写什么代码,以便列表可以继续进行,如1.10和1.11,而不是在1.9之后立即进入2。
提前感谢。
英文:
I'm running some SAS code on a table sorted by groups with multiple entries in each group. I want to give all the entries a unique identifier based on a sort order. For example:
Group Item Label
A Z 1.1
A Y 1.2
B Z 2.1
B Y 2.2
Many of the groups have more than 10 items, but I dont want the count to just roll over to the next integer value. What can I code that will keep the list going into things like 1.10 and 1.11 instead of going right to 2 after 1.9.
Thanks in advance.
答案1
得分: 2
使用按组处理和sum语句来递增数值:对于每个组,以及对于每个组内的项目都要递增。你可以通过连接这两者并用 .
分隔它们来创建一个唯一的标识变量,名为 "label"。确保首先按 group
进行排序。
data want;
set have;
by group;
/* 递增组标签并在每个组的开头重置项目编号 */
if(first.group) then do;
group_num+1;
item_num=0;
end;
/* 递增项目编号 */
item_num+1;
label = cats(group_num, '.', item_num);
drop group_num item_num;
run;
group item label
A Z 1.1
A Y 1.2
B Z 2.1
B Y 2.2
英文:
Use by-group processing and the sum statement to increment values: once for each group, and once for each item within the group. You can create a unique identifier variable named "label" by concatenating these two together and separating them with a .
. Be sure it's sorted by group
first.
data want;
set have;
by group;
/* Increment the group label and reset the item num at the start
of each group */
if(first.group) then do;
group_num+1;
item_num=0;
end;
/* Increment the item number */
item_num+1;
label = cats(group_num, '.', item_num);
drop group_num item_num;
run;
group item label
A Z 1.1
A Y 1.2
B Z 2.1
B Y 2.2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论