我很难根据客户提供给我的其他变量的规则来减少计数变量。
当前,我有“ HAVE”列,但想实现“ WANT”
我们知道此ID有5个唯一的日期更改,并且忽略了同一天的重复值-这将显示我随着时间的变化有5个唯一值。
这是一个例子,“有”是我的尝试,“有”是期望的结果(我认为)。
DATA HAVE;
LENGTH
ID 8 DATE_OF_VALUE $10 VALUE 8 HAVE 8 WANT 8 ;
FORMAT
ID 9. DATE_OF_VALUE $10. VALUE 5. HAVE 1. WANT 1. ;
INFORMAT
ID 9. DATE_OF_VALUE $10. VALUE 5. HAVE 1. WANT 1. ;
INFILE DATALINES4
DLM=','
MISSOVER
DSD ;
INPUT
ID : 9. DATE_OF_VALUE : $10. VALUE: 5. HAVE: 1.WANT : 1. ;
DATALINES4;
109999999,05/12/2017,42376,1,5
109999999,05/12/2017,42376,2,.
109999999,13/12/2017,42521,1,4
109999999,13/12/2017,42521,2,.
109999999,19/03/2018,43395,1,3
109999999,19/03/2018,43395,2,.
109999999,19/03/2018,43395,3,.
109999999,19/03/2018,43395,4,.
109999999,04/06/2018,43953,1,2
109999999,04/06/2018,43953,2,.
109999999,17/09/2018,44899,1,1
109999999,17/09/2018,44899,2,.
109999998,21/04/2018,4788,1,2
109999998,21/04/2018,4788,2,.
109999998,01/06/2018,4788,3,.
109999998,01/06/2018,4788,4,.
109999998,23/09/2018,4718,1,1
109999998,23/09/2018,4718,2,.
109999997,09/07/2018,8717,1,2
109999997,09/07/2018,8717,2,.
109999997,01/09/2018,8750,1,1
109999997,01/09/2018,8750,2,.
109999995,15/02/2018,22868,1,2
109999995,15/02/2018,22868,2,.
109999995,07/09/2018,22631,1,1
109999995,07/09/2018,22631,2,.
109554995,05/12/2017,92376,1,5
109554995,05/12/2017,92376,2,.
109554995,13/12/2017,92521,1,4
109554995,13/12/2017,92521,2,.
109554995,19/03/2018,93395,1,3
109554995,19/03/2018,93395,2,.
109554995,19/03/2018,93395,3,.
109554995,19/03/2018,93395,4,.
109554995,04/06/2018,93953,1,2
109554995,04/06/2018,93953,2,.
109554995,11/07/2018,94953,1,1
;;;;
答案 0 :(得分:0)
一种解决方法是使用所谓的双DOW循环。
您两次读取了ID组中的数据。您将在代码中看到两个do循环。第一个读取ID组中的记录以计算唯一日期的数量。然后,第二个重新读取相同的记录,并使用递减的计数器将其记录输出。
请注意,在我所拥有的代码中,我将您的date_of_value变量读取为数字日期变量,以便对其进行排序。如果要将其保留为字符,则可以添加proc排序步骤。
data want ;
*Count the number of distinct for an ID group;
do until (last.id) ;
set have (keep=ID Date_Of_Value Value rename=(Date_Of_value=Date));
by descending id date ;
if first.date then _DateCount=sum(_DateCount,1) ;
end ;
*Re-read the records for the ID group, decrementing the counter and outputting ;
do until(last.id) ;
set have (keep=ID Date_Of_Value Value rename=(Date_Of_value=Date));
by descending id date ;
if first.id then Want=_DateCount ;
else if first.date then Want=Want-1 ;
output ;
end ;
run ;
这会给出购买组中WANT的重复值,这与WANT不同,在WANT之后,WANT在重复的日期后缺少值:
ID Date VALUE Wan
109999999 21158 42376 5
109999999 21158 42376 5
109999999 21166 42521 4
109999999 21166 42521 4
109999999 21262 43395 3
109999999 21262 43395 3
109999999 21262 43395 3
109999999 21262 43395 3
109999999 21339 43953 2
109999999 21339 43953 2
109999999 21444 44899 1
109999999 21444 44899 1
109999998 21295 4788 3
109999998 21295 4788 3
109999998 21336 4788 2
109999998 21336 4788 2
109999998 21450 4718 1
109999998 21450 4718 1
109999997 21374 8717 2
109999997 21374 8717 2
109999997 21428 8750 1
109999997 21428 8750 1
109999995 21230 22868 2
109999995 21230 22868 2
109999995 21434 22631 1
109999995 21434 22631 1
109554995 21158 92376 5
109554995 21158 92376 5
109554995 21166 92521 4
109554995 21166 92521 4
109554995 21262 93395 3
109554995 21262 93395 3
109554995 21262 93395 3
109554995 21262 93395 3
109554995 21339 93953 2
109554995 21339 93953 2
109554995 21376 94953 1
我倾向于不喜欢缺失的值,但是如果需要,您可以随意更新它以创建缺失的值。 :)