Date: Wed, 20 May 1998 03:38:09 GMT
Reply-To: jshi@KUHUB.CC.UKANS.EDU
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Jingren Shi <jshi@KUHUB.CC.UKANS.EDU>
Organization: Deja News - The Leader in Internet Discussion
Subject: Code comparison
Sorry, I forget who posted the message for code comparion. I lost my e-mail
message.
To create datasets upon a value of a given variable, here is the code. It is
very nice one.
data test;
length value $2.;
letters = 'abcdefghijklmnopqrstuvwxyz'; drop letters;
do i = 1 to 26;
do j = 1 to 2;
value = substr(letters, i, 1) || substr(letters, j, 1);
/* Not always the same number of obs with each value */
do k = 1 to i*j;
output;
end;
end;
end;
*****; run;
/* Find out what the different values are, and put */
/* them into macro variables. DSNAMES will contain */
/* the names of the output datasets (DS_<value> in */
/* this case, but you could calculate it however you */
/* like), and DSWHENS will contain WHEN clauses for */
/* a select statement. */
%global DSNAMES DSWHENS;
proc sql noprint;
select distinct
count(value) as vcount,
'DS_' || value,
"when ('" || value || "') output DS_" || value || ";"
into :DSCNTS, /* Isn't used, but syntactically required */
:DSNAMES separated by ' ',
:DSWHENS separated by ' '
from test
group value
order vcount descending;
*****; quit;
/* Create new datasets. */
options symbolgen;
data &DSNAMES.;
set test;
select(value);
&DSWHENS.
end;
/* An interesting problem is that you CANNOT have a */
/* semicolon after DSWHENS - you'll get an error. An */
/* OTHERWISE isn't needed, because you've accounted */
/* for every possible value. */
*****; run;
I use data step instead of proc sql. I attach it here,
data test;
length value $2.;
letters = 'abcdefghijklmnopqrstuvwxyz'; drop letters;
do i = 1 to 26;
do j = 1 to 2;
value = substr(letters, i, 1) || substr(letters, j, 1);
/* Not always the same number of obs with each value */
do k = 1 to i*j;
output;
end;
end;
end;
*****; run;
proc sort data=test out =testsort nodupkey;by value;run;
data _null_; set testsort end=eof; call
symput('dsn'||left(_n_),'ds_'||value); call symput('dsw'||left(_n_),'when
("'||value||'") output DS_'||upcase(value)||';'); if eof then call
symput('cnts',_n_); run;
%macro doit;
data %do i=1 %to &cnts;
&&dsn&i
%end;
;
set test;
select (value);
%do i=1 %to &cnts;
&&dsw&i
%end;
otherwise;
end;
run;
%mend;
%doit;
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading