Date: Wed, 28 Oct 2009 14:40:15 -0700
Reply-To: "Anderson, James" <James.Anderson@UCSF.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Anderson, James" <James.Anderson@UCSF.EDU>
Subject: Re: Any trick to get SAS variable name?
Content-Type: text/plain; charset="iso-8859-1"
As a shorter alternative to the non-macro code below, you can use the %for macro as follows:
data mydata_recode;
set mydata;
%for(name type, in={mydata}, do=%nrstr(
%if &type=2 %then
%do; %*character variable;
&name._1 = substr(&name,1,1);
&name._2 = substr(&name,2,1);
%end;
))
run;
Above, {mydata} means iterate over the equivalent of proc contents. The macro variable &name is set to a mydata dataset variable name and the macro variable &type is set to 1 for numeric and 2 for character variables.
You can find the %for macro and a description of how to use it here http://20.fi/2843
Jim
-----Original Message-----
From: Dale McLerran [mailto:stringplayer_2@YAHOO.COM]
Sent: Wednesday, October 28, 2009 11:24 AM
Subject: Re: Any trick to get SAS variable name?
As an alternative to using macro code, one could use
CALL EXECUTE as shown below. All code generated in
CALL EXECUTE statements is executed after the current
data step completes. Thus, we use a data step to write
another data step which is subsequently executed. The
driver data step places the SNPs in an array and loops
over that array to determine the names of the variables
in the array. In the code which the driver data step
generates, the generated code concatenates a position
index to the SNP name. The driver data step also
generates code to fill in the appropriate value.
data _null_;
set mydata(obs=1);
array SNPS {*} _character_;
call execute("data mydata_recode;");
call execute(" set mydata;");
/* Original SNP name list in an array */
call execute(" array SNPS{*} _character_;");
/* Construct variables "varx1 varx2 vary1 ... varz2" */
/* and put all of those variables into an array. The */
/* VNAME function is used to extract the SNP name. */
/* We append first a 1 and then a 2 to the SNP name */
/* to construct the variables which we need. */
call execute(" array SNPS_rev{*} $ ");
do i=1 to dim(SNPS);
call execute(compress(put(vname(snps{i}),$31.) || "1"));
call execute(compress(put(vname(snps{i}),$31.) || "2"));
end;
call execute(" ;");
/* Loop over the SNP array and the half SNP array. */
/* For the SNP array, there are two variables in the */
/* half SNP array. For the first half SNP, select */
/* the portion of the SNP in position 1. For the */
/* second half SNP, select the portion of the SNP in */
/* position 2. */
call execute(" var_counter=0;");
call execute(" do i=1 to dim(SNPS);");
call execute(" do j=1 to 2;");
call execute(" var_counter+1;");
call execute(" SNPS_rev{var_counter} = substr(SNPS{i},j,1);");
call execute(" end;");
call execute(" end;");
/* Drop original SNP variables and indexing variables */
call execute(" drop ");
do i=1 to dim(SNPS);
call execute(vname(snps{i}));
end;
call execute(" i j var_counter;");
call execute("run;");
run;
proc print data=mydata_recode;
run;
HTH,
Dale
---------------------------------------
Dale McLerran
Fred Hutchinson Cancer Research Center
mailto: dmclerra@NO_SPAMfhcrc.org
Ph: (206) 667-2926
Fax: (206) 667-5977
---------------------------------------
--- On Wed, 10/28/09, Fernández Rodríguez, Dani <DFernandez@CST.CAT> wrote:
> From: Fernández Rodríguez, Dani <DFernandez@CST.CAT>
> Subject: Re: Any trick to get SAS variable name?
> To: SAS-L@LISTSERV.UGA.EDU
> Date: Wednesday, October 28, 2009, 5:43 AM
> Hi duo_wan,
>
>
>
> I have created the macro %two_var the most efficient I
> could:
>
>
>
> data test;
>
> input varx $ vary $ varz $;
>
> cards;
>
> AG CT AC
>
> ;;
>
> run;
>
> *************************;
>
> %let libname= work;
>
> %let table= test;
>
> *************************;
>
> %macro two_var;
>
> %let dsn=%sysfunc(open(&libname..&table,i));
>
> %do i=1 %to %sysfunc(attrn(&dsn,nvars)); %if
> %sysfunc(vartype(&dsn,&i))=C %then %do; %do n=1 %to
> 2; %let vname=%sysfunc(varname(&dsn,&i)); %let
> dsnvar&n
> =%sysfunc(cats(%sysfunc(varname(&dsn,&i)),_,&n));
>
> &&dsnvar&n.=substr(&vname,&n,1);
>
> %end;
>
> %end;
>
> %end;
>
> %let rc=%sysfunc(close(&dsn));
>
> %mend;
>
>
>
> data need;
>
> set test;
>
> %two_var;
>
> drop varx--varz;
>
> run;
>
>
>
>
>
>
>
> I hope it is helpful.
>
> Daniel Fernandez.
>
> Barcelona.
>
>
>
> -----Mensaje original-----
>
> De: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU]
> En nombre de duo wan Enviado el: dimarts, 27 / octubre /
> 2009 23:57
>
> Para: SAS-L@LISTSERV.UGA.EDU
>
> Asunto: SUSPECT: Any trick to get SAS variable name?
>
>
>
> Hi All,
>
>
>
> I am working on a data set and would like to break each
> variable(SNP) into two variables with similar name. for
> example:
>
>
>
> original data:
>
>
>
> varx vary varz... ~1000 variables
>
> AG CT AC
>
>
>
> new data:
>
>
>
> varx_1, varx_2, vary_1, vary_2....
>
> A G C T
>
>
> I am thinking about using Arrays (array snp{*} _all_} and
> macro variables. Is there anyway to know the variable name
> of ith element in array "SNP"?
>
> thanks,
>
> Vincent
>
|