在SAS中,当行组变化时,我需要一个计数变量。

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

In SAS I need counter variable when the group of rows changes

问题

我想在 SAS 中每当篮子的价值变化时创建一个 ID。

在SAS中,当行组变化时,我需要一个计数变量。

我尝试过使用哈希和首次。通过计数+1,但它们都将组 A 显示为一个 ID。

英文:

I want to create ID in SAS every time the basket value changes.

在SAS中,当行组变化时,我需要一个计数变量。

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;

huangapple
  • 本文由 发表于 2023年4月20日 03:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058121.html
匿名

发表评论

匿名网友

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

确定