2014-03-27 4 views
1

SAS에 대해 처음 접했기 때문에 출발점을 찾을 수 없습니다.하나의 변수에서 많은 변수를 추출하는 SAS 매크로

customer id number volume 
1  ab 10  5 
1  cd 7  3 
2  xy 15  2 
2  ab 3  50 

는 내가 모든 distint ID에 대한 새 변수를 필요로하는 새로운 데이터 세트를 만들려면 :

나는 다음과 같다 데이터 집합을 가지고있다. id가 나타나는 행에서 숫자와 볼륨이 새 변수에 곱 해져야합니다. 새 데이터 세트는 다음과 같아야합니다.

customer id number volume ab cd xy 
1  ab 10  5  50 . . 
1  cd 7  3  . 21 . 
2  xy 15  2  . . 30 
2  ab 3  50 150 . . 

누구나 아이디어가 있습니까? 어쩌면 솔루션은 정말 쉽지만, SAS에 대한 새로운 경험이 있기 때문에 모든 의견을 환영합니다.

답변

4

매크로를 사용할 필요가 없습니다. 배열은 이것을 처리해야합니다. 당신이 그들 중 많은 경우

data want; 
set have; 
array flagvars ab cd xy; *an array of your 3 new variables; 
do _i = 1 to dim(flagvars); *iterate one to the dimension of the array (# of vars in it); 
    if upcase(vname(flagvars[_i])) = upcase(id) then flagvars[_i] = number*volume; *if the name of the variable is identical to the id value, set that member of the array to the desired value; 
end; 
run; 

, 당신은 배열 변수의 목록이있는 매크로 변수를 구성 할 수 있습니다 :

proc sql; 
select distinct id into :idlist separated by ' ' from have; 
quit; 

다음의 장소에

array flagvars &idlist.; 

을 사용하는 기입 명부.

숫자 * 볼륨 변수를 미리 작성하면 PROC TRANSPOSE 할 수 있습니다.

data have; 
input customer id $ number volume; 
total = number*volume; 
datalines; 
1  ab 10  5 
1  cd 7  3 
2  xy 15  2 
2  ab 3  50 
;;;;; 
run; 
proc sort data=have; 
by customer id; 
run; 
proc transpose data=have out=have_t; 
by customer id; 
copy number volume; 
var total; 
id id; 
run; 

ID 문 이름 (이 경우, id) 인수에 따라 변수. Copy은 조 변경에 사용되지 않는 변수를 추가합니다. 제대로 작동하려면 customer id으로 정렬해야합니다.

+0

빠른 답변 주셔서 감사합니다. 하지만 다른 어리석은 질문이 있습니다 ;-) 실제로 952 개의 다른 ID가 있으므로 직접 코드에 쓸 수는 없습니다. 사전에 배열에 저장할 수 있습니까? – Tess

+0

두 번째 솔루션과 해당 목록을 자동으로 가져 오는 방법을 편집했습니다. – Joe

+0

완벽하게 작동합니다! 정말 고마워!!! – Tess