f



Re: Assign variables to a new variable based on variable names in SAS #3

Here's a solution that builds a list of subscripts in macro variables named
like the 'a_' columns in the input dataset.  'a_' fields are referenced by
the subscript from the array they're defined to be part of:


data stuff;
  a_2003q4 = 44;
  a_2003q1 = 11;
  a_2003q2 = 22;
  a_2003q3 = 33;
  n = 'a_2003q3';
run;

proc sql noprint;
    create table columns as
      select name
        from sashelp.vcolumn
      where libname = 'WORK'
        and memname = 'STUFF'
        and name  eqt 'a_'
      order by name
    ;
    select compbl('%let ' || name || ' = ' || put(monotonic(),3.))
      into :sub_assign separated by ';'
      from columns
    ;
    select name into: a_names separated by ' '
      from columns
    ;
quit;

options symbolgen;

&sub_assign;

data new ( drop = _: );
   set stuff;
   array a{*} &a_names ;
   _sub = symget(n);
   if lbound(a) <= _sub <= hbound(a) then
       new_a = a(_sub);
run;

proc print;
run;

-----Original Message-----
From: W M [mailto:mwx74@HOTMAIL.COM]
Sent: January 15, 2004 2:02 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Assign variables to a new variable based on variable names in
SAS


Thank so much Nagakumar Sridhar and  Fehd, Ronald J. for answering my
previous
question. However I was not clear in the original post. I modified my
phrasing
and the new version is below:



I have met the following problem and I need your expert help.
The variable names in the data set looks like this:

a_1995q3   a_1995q4  a_1997q1 ........... a_2003q4  n

Please note that "quarter" numbers in "a_****q*" variables may not be
continuous, i.e. there might be a missing quarter.

and n can have the value of any quarter for while a_quarter exists,
say
"1995q3", "20003q4".

I need to do this:

 if n="1995q3"  then new_a=a_1995q3;
...............
if n="2003q4" then new_a=a_2003q4;

I can use array to do this; but as time goes by, we will add variable
a_2004q1
to the list. Is there a way like

new_a=a_{value of n} ?

Because of the compilation of data step, you cannot do

data new; set old;
call symput("name", n);
new_a=a_&n; (wrong !)
run;

Thanks so much, V.
0
1/15/2004 8:00:33 PM
comp.soft-sys.sas 142828 articles. 3 followers. Post Follow

0 Replies
960 Views

Similar Articles

[PageSpeed] 26

Reply: