英文:
In SAS I need counter variable when the group of rows changes
问题
我想在 SAS 中每当篮子的价值变化时创建一个 ID。
我尝试过使用哈希和首次。通过计数+1,但它们都将组 A 显示为一个 ID。
英文:
I want to create ID in SAS every time the basket value changes.
I tried hash and first. with count+1 but all of them shows group A as one ID.
答案1
得分: 1
使用BY组处理。由于您的BASKET值似乎已分组但实际上并未排序,请使用NOTSORTED关键字。SUM语句是创建一个保留其值的新变量的简单方法。
data want;
set have ;
by loan basket NOTSORTED ;
id + first.basket ;
run;
英文:
Use BY group processing. Since your BASKET values appear to be grouped but not actually sorted use the NOTSORTED keyword. A SUM statement is a simple way to create a new variable whose value is retained.
data want;
set have ;
by loan basket NOTSORTED ;
id + first.basket ;
run;
答案2
得分: 0
I worked with some data I made up myself:
data HAVE;
do Loan_no = 123 to 234 by 7;
do Basket = "A", "B", "C", "D";
do none = "there", "we", "go";
do sense = 1 to 2;
output;
end;
end;
end;
end;
run;
As variables are by default re-initialized for each observation, you need to retain
your counter and initialize it to zero.
To detect the beginning of a group, with first.Basket
, you need to process the data by Loan_no Basket
, but not require the dataset to be sorted alphabetically.
Then the solution is:
data WANT;
set have;
retain id 0;
by Loan_no Basket notsorted;
if first.Basket then id = id + 1;
run;
英文:
I worked with some data I made up myself:
data HAVE;
do Loan_no = 123 to 234 by 7;
do Basket = "A", "B", "C", "D";
do none = "there", "we", "go";
do sense = 1 to 2;
output;
end;
end;
end;
end;
run;
As variables are by default re-initialized for each observation, you need to retain
your counter and initialise it to zero .
To detect the beginning of a group, with first.Basket
, you need to process the data by Load_no Basket
, but not require the dataset to be sorted alphabetically.
Then the solution is
data WANT;
set have;
retain id 0;
by Loan_no Basket notsorted;
if first.Basket then id = id + 1;
run;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论