在SAS中遍历数据以添加类似于”1.9, 1.10, 1.11″的标签。

huangapple go评论83阅读模式
英文:

SAS Loop through data to add labels in the style of "1.9, 1.10, 1.11" etc

问题

我正在运行一些 SAS 代码,对一个按组排序并且每个组内有多个条目的表执行操作。我希望根据排序顺序为所有条目分配唯一的标识符。例如:

  1. 项目 标签
  2. A Z 1.1
  3. A Y 1.2
  4. B Z 2.1
  5. 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:

  1. Group Item Label
  2. A Z 1.1
  3. A Y 1.2
  4. B Z 2.1
  5. 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 进行排序。

  1. data want;
  2. set have;
  3. by group;
  4. /* 递增组标签并在每个组的开头重置项目编号 */
  5. if(first.group) then do;
  6. group_num+1;
  7. item_num=0;
  8. end;
  9. /* 递增项目编号 */
  10. item_num+1;
  11. label = cats(group_num, '.', item_num);
  12. drop group_num item_num;
  13. run;
  1. group item label
  2. A Z 1.1
  3. A Y 1.2
  4. B Z 2.1
  5. 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.

  1. data want;
  2. set have;
  3. by group;
  4. /* Increment the group label and reset the item num at the start
  5. of each group */
  6. if(first.group) then do;
  7. group_num+1;
  8. item_num=0;
  9. end;
  10. /* Increment the item number */
  11. item_num+1;
  12. label = cats(group_num, '.', item_num);
  13. drop group_num item_num;
  14. run;
  1. group item label
  2. A Z 1.1
  3. A Y 1.2
  4. B Z 2.1
  5. B Y 2.2

huangapple
  • 本文由 发表于 2023年4月11日 00:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定