sas >> Can I use array to rename variables?

by sophiDT » Wed, 18 Apr 2007 02:07:53 GMT

I have a set of variables need to be renamed, I tried the following code and
get error message, not sure if it's not feasible or just simply a syntax error.

Please offer your expertise,Many thanks in advance.


%LET NAME=var1 var2 ....varn;
DATA PEGT3A;
SET PEGT3;
ARRAY VARN [*] &NAME;
DO I=1 TO DIM(VARN);
RENAME VARN[I]=VARN[I] || '__'||3;
END;
RUN;

LOG:

RENAME VARN[I]=VARN[I] || '__'||3;
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.

ERROR 76-322: Syntax error, statement will be ignored.


sas >> Can I use array to rename variables?

by johbee » Wed, 18 Apr 2007 02:31:14 GMT


Not really sure what you're after here, there are other ways to do the
rename.
Perhaps the following will help you to clarify the question.

dm 'log' clear;
dm 'out' clear;

%let name=%str($varn1-varn3);

data pegt3;
length var1-var3 $10;
var1='1'; var2='2'; var3='3'; output;
run;

data pegt3a(drop=i var1-var3);
set pegt3;
array var[*] $var1-var3;
array varn[*] &name;
do i=1 to dim(var);
varn[i]=compress(var[i]|| '__'||'3');
end;
run;


proc print data=pegt3a;
run;






sas >> Can I use array to rename variables?

by datanull » Wed, 18 Apr 2007 02:39:15 GMT

RENAME is not executable. You could do something like this to
accomplish what your program appears to be trying to do.

data work.test;
attrib var1-var30 length=$1;
call missing(of var:);
run;
%let renameList = ;
proc sql noprint;
select cats(name,'=',name,'_3') into :renameList separatedBy ' '
from dictionary.columns
where libname eq 'WORK' and memname eq 'TEST' and name like 'var%'
;
quit;
run;
%put _user_;
proc datasets library=work;
modify test;
rename &renamelist;
run;
contents data=test varnum;
run;
quit;


However, I would advise against putting _3 on the end of the name as
your program implies. Because it will cripple the name range. If you
have V9 and put the 3 in the name before the variable numbers you can
do something like the following.

Much easier.

data work.test;
attrib var1-var30 length=$1;
call missing(of var:);
rename var1-var30 = var3_1-var3_30;
run;
proc contents varnum;
run;








Can I use array to rename variables?

by Huang.JS » Wed, 18 Apr 2007 03:00:59 GMT

little macro might help:

data PEGT3;
input var1 var2 var3;
datalines;
1 2 3
4 5 6
7 8 9
;

proc print data=PEGT3;
run;

%macro Rename(n);
%global rename;
%let rename=;
%do i=1 %to &n;
%let rename=&rename var&i=var&i.__3;
%end;
%mend;

%Rename(3);

DATA PEGT3A;
SET PEGT3;
RENAME &rename;
RUN;

proc print data=PEGT3A;
run;

***** Output *****
The SAS System 11:12
Tuesday, April 17, 2007 30

Obs var1 var2 var3

1 1 2 3
2 4 5 6
3 7 8 9

The SAS System 11:12
Tuesday, April 17, 2007 31

Obs var1__3 var2__3 var3__3

1 1 2 3
2 4 5 6
3 7 8 9

-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
Sophia Tong
Sent: Tuesday, April 17, 2007 1:08 PM
To: XXXX@XXXXX.COM
Subject: Can I use array to rename variables?

I have a set of variables need to be renamed, I tried the following code
and get error message, not sure if it's not feasible or just simply a
syntax error.

Please offer your expertise,Many thanks in advance.


%LET NAME=var1 var2 ....varn;
DATA PEGT3A;
SET PEGT3;
ARRAY VARN [*] &NAME;
DO I=1 TO DIM(VARN);
RENAME VARN[I]=VARN[I] || '__'||3;
END;
RUN;

LOG:

RENAME VARN[I]=VARN[I] || '__'||3;
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.

ERROR 76-322: Syntax error, statement will be ignored.


-----Message Disclaimer-----

This e-mail message is intended only for the use of the individual or
entity to which it is addressed, and may contain information that is
privileged, confidential and exempt from disclosure under applicable law.
If you are not the intended recipient, any dissemination, distribution or
copying of this communication is strictly prohibited. If you have
received this communication in error, please notify us immediately by
reply email to XXXX@XXXXX.COM and delete or destroy all copies of
the original message and attachments thereto. Email sent to or from the
Principal Financial Group or any of its member companies may be retained
as required by law or regulation.

Nothing in this message is intended to constitute an Electronic signature
for purposes of the Uniform Electronic Transactions Act (UETA) or the
Electronic Signatures in Global and National Commerce Act ("E-Sign")
unless a specific statement to the contrary is included in this message.

While this communication may be used to promote or market a transaction
or an idea that is discussed in the publication, it is intended to provide
general information about the subject matter covered and is provided with
the understanding that The Principal is not rendering legal, accounting,
or tax advice. It is not a marketed opinion and may not be used to avoid
penalties under the Internal Revenue Code. You should consult with
appropriate counsel or other advisors on all matters pertaining to legal,
ta

Can I use array to rename variables?

by johbee » Thu, 19 Apr 2007 01:03:30 GMT

orry, I thought you wanted an rename and a value change. Another example
-similiar to the previous:


data pegt3;
length var1-var3 $10;
var1='1'; var2='2'; var3='3'; output;
run;

%macro makeds;
data _null_;
set pegt3;
array var[*] $var1-var3;
call symput('x',dim(var));
run;

data pegt3a;
set pegt3;
array var[*] $var1-var3;
%do i=1 %to &x;
rename var&i=varn&i._3;
%end;
run;

proc print data=pegt3a;
run;
%mend makeds;

%makeds




On 4/17/07, Huang, JS < XXXX@XXXXX.COM > wrote:


Similar Threads

1. rename array variable

I need to rename d1 and d2 using arrays, in real problem I have 100
variables, so basically can not write simple lable, also I'm trying to
avoid macro, to minimize code changes. Any ideas?

data t;
input d1 d2;
cards;
1 2
2 3
3 4
;
run;



data tt;
   set t;
   array names{*} d1-d2;
   array capitals{*} $ c1-c2 ('0-2 months' '10-12 months');
      do i=1 to 2;
         label names{i}=capitals{i};
      end;

run;


Log file error:
----------------
658      set t;
659     array names{*} d1-d2;
660     array capitals{*} $ c1-c2 ('0-2 months' '10-12 months');
661        do i=1 to 2;
NOTE: SCL source line.
662           label names{i}=capitals{i};
                         -
                         73
                         76
ERROR 73-322: Expecting an =.

ERROR 76-322: Syntax error, statement will be ignored.

2. Using Sql Views to Rename, Order and Change Variable Type

3. Using renamed variable labels in PDF output

I have used the 'Label' commmand in a datastep to rename some variable
names.

I then want to output this dataset to a PDF file but the PDF retains
the old label names.

I have also triend using the 'Label' command in the Proc Print in the
ODS code, but again this doesn't seem to work.....


Thanks

4. Using Arrays - How to transpose two variables at a time

5. QUERY: Construction of variable-dimension arrays using macro

> From: Dennis Diskin
>  %let varlen=5;
> Data try;
> Array element(*) a1-a&varlen;
> Run;
>  HTH,
> Dennis Diskin

in addition this will work:

Data try;
Array a(&varlen);*creates a1-a&varlen;
Run;
PROC SQL;describe table work.try;quit;

Ron Fehd  the macro maven  CDC Atlanta GA USA RJF2 at cdc dot gov

> On 11/16/05, Stephan Rudolfer
> < XXXX@XXXXX.COM > wrote:
> >
> > Let len denote a positive integer. I wish to create an
> array of dimension
> > len. There is a problem here: to create an array, we have
> to specify the
> > first and last elements in the array. However, here the number is a
> > variable, and SAS doesn't seem able to accept an array
> element of the form
> > a&len. Naively, I've tried the following code:
> >
> > Data try;
> > Len=5;
> > %let varlen=len;
> > Array element(len) a1-a&varlen;
> > Run;
> >
> > This generates the following log file:
> >
> > 153 data try;
> > 154 len=5;
> > 155 %let varlen=len;
> > 156 %let last=lot&varlen;
> > SYMBOLGEN: Macro variable VARLEN resolves to len
> > 157 array element(len)
> > 157! a1-a&varlen;
> > SYMBOLGEN: Macro variable VARLEN resolves to len
> > ERROR: Missing numeric suffix on a numbered variable list (a1-alen).
> > WARNING: Defining an array with zero elements.
> > 158 run;
> >
> > What I was hoping was that the macro substitution would
> generate a-len=a5.
> >
> > Any suggestions would be most gratefully received.
> >
>

6. inserting values into macro variable using array

7. using array variables as part of call to macro function

Hi:
 I have a macro function called output that takes a dataset as
argument and i have datasets: dataset_1a, dataset_2a, etc, and I want
to call that macro with the corresponding dataset name and store the
result as new variable in xx1.

data xx;
  set xx1;
  array vars{3} (1,2,3);
  array chars{3}$ ('a','b','c');
  do i = 1 to dim(vars);
   do j=1 to dim(vars)
    tmp = %output(input=dataset_vars(i)chars(j));
  end;
  drop tmp;
run;

and SAS is giving me: ERROR: Invalid option name x(whatever i is)

how can i fix this? thank you

8. Renaming Column using an observation