0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SAS言語備忘録_縦持ちから横持ちへ

Posted at

#transposeを知らなかった自分
縦持ちデータを横持ちデータに変換したいニーズがあり当初はExcelVBAで実現したものの、組織の標準ソフトであるSASで実現した時の話です。
当時はproc transposeを知らずsortからのretainで追番振ってデータセットを追番の最大値の数だけ分割してひたすらマージという初心者ムーヴっぷり。
自戒のためにも晒しておきます。

sample.sas
data WORK.NUMBERING;
	set WORK.DATA;
	by KEY;
	retain NO;
	if first.KEY then NO = 0;
	NO + 1;
run;

proc means data=WORK.NUMBERING max noprint;
	var NO;
	output out=WORK.MAX MAX=MAX;
run;

data _null_;
	length MAX 8;
	set WORK.MAX;
	call symputx("MAX",MAX);
run;

%macro test(num);
	data WORK.DATA&num.;
		set WORK.NUMBERING;
		if NO = &num. then output;
	run;
%mend test;

%macro test2(num);
	proc datasets library=WORK nolist;
		modify DATA&num.;
		rename KNJ = KNJ_&num. NO = NO_&num.;
	quit;
%mend test2;

%macro make_data;
	%do i=1 %to &MAX.;
		%test(&i.);
		%test2(&i.);
	%end;
%mend make_data;

%make_data;

data WORK.M_DATA1;
	merge WORK.DATA1
		  WORK.DATA2;
	by KEY;
run;

%macro merge_data;
	
	%do j=2 %to %eval(&MAX.-1);

		data WORK.M_DATA&j.;
			merge WORK.M_DATA%eval(&j.-1)
				  WORK.DATA%eval(&j.+1);
			by KEY;
		run;

	%end;

%mend;

%merge_data;

data WORK.COMP_DATA;

	set WORK.M_DATA%eval(&MAX.-1)(drop=NO_1);	

run;

%macro drop_no;

	%do k=2 %to &MAX.;

		data WORK.COMP_DATA;
			set WORK.COMP_DATA(drop=NO_&k.);
		run;

	%end;

%mend;

%drop_no;
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?