sas >> Concatenating variables with same prefix or suffix

by sanjivet2k6 » Sat, 30 May 2009 01:51:47 GMT

Hi, Is there a way we could concatenate variables with either same prefix o=
r suffix in a dataset. (NOT concatenating datasets).=20
=A0
Also, did anyone try getting Mean(S. D.) or Min, Max (customized stats) fro=
m proc tabulate and would like to share the information. (Nope, not using d=
ata _null_ and concatenating, but getting an output from proc tabulate?)
=A0
Thanks very much.
sk=0A=0A=0A Cricket on your mind? Visit the ultimate cricket website. =
Enter http://beta.cricket.yahoo.com


sas >> Concatenating variables with same prefix or suffix

by snoopy369 » Sat, 30 May 2009 02:11:35 GMT


You might want to be a wee bit more specific - like give example data.

In general, yes, there are ways to concatenate variables with similar
suffix/prefix.

Prefix is easiest:

data test;
x1=2;
x2=3;
x3=4;
run;

data test2;
set test;
x = cats(of x:);
run;

Suffix usually requires some array manipulation or a proc sql macro call.
Which makes more sense depends on your data.

-Joe


On Fri, May 29, 2009 at 12:51 PM, Sanjeev Kommera





sas >> Concatenating variables with same prefix or suffix

by sanjivet2k6 » Sat, 30 May 2009 02:24:51 GMT

Thanks Joe, I should have been more clear. example variables will be xxx_ a=
nd _xxx not col1-col&maxnum
=A0
Thanks=20




From: Joe Matise < XXXX@XXXXX.COM >
Subject: Re: Concatenating variables with same prefix or suffix
To: "Sanjeev Kommera" < XXXX@XXXXX.COM >
Cc: XXXX@XXXXX.COM
Date: Friday, 29 May, 2009, 11:41 PM


You might want to be a wee bit more specific - like give example data.

In general, yes, there are ways to concatenate variables with similar suffi=
x/prefix.

Prefix is easiest:

data test;
x1=3D2;
x2=3D3;
x3=3D4;
run;

data test2;
set test;
x =3D cats(of x:);
run;

Suffix usually requires some array manipulation or a proc sql macro call.=
=A0 Which makes more sense depends on your data.

-Joe



On Fri, May 29, 2009 at 12:51 PM, Sanjeev Kommera < XXXX@XXXXX.COM >=


Hi, Is there a way we could concatenate variables with either same prefix o=
r suffix in a dataset. (NOT concatenating datasets).
=A0
Also, did anyone try getting Mean(S. D.) or Min, Max (customized stats) fro=
m proc tabulate and would like to share the information. (Nope, not using d=
ata _null_ and concatenating, but getting an output from proc tabulate?)
=A0
Thanks very much.
sk


=A0 =A0 =A0Cricket on your mind? Visit the ultimate cricket website. Enter =
http://beta.cricket.yahoo.com

=0A=0A=0A Cricket on your mind? Visit the ultimate cricket website. En=
ter http://beta.cricket.yahoo.com


Concatenating variables with same prefix or suffix

by snoopy369 » Sat, 30 May 2009 02:33:05 GMT

Doesn't matter... : does anything:

xxx_:
means all variables that start with xxx_

_xxx is harder, you need to do some computations either with arrays:

array vars _numeric_;
do _n_ = 1 to dim(vars);
if find(vname(vars[_n_]),'_xxx') then concatvar =
cats(concatvar,vars[_n_]);
end;

or with a macro list, usually using proc sql to generate it. Something
like:

proc sql;
select name into :namelist separated by ' '
from dictionary.columns
where libname="<libarary name>"
and memname="<datasetname>"
and find(name,"_xxx") > 0;
quit;

will do, and then
cats(&namelist)
will give you the result you want.

I suggest using UPCASE in the proc sql, if you end up going that route, as
the upcase/lowcase of variable names etc. is not alwasy consistent.

-Joe







Concatenating variables with same prefix or suffix

by msz03 » Sat, 30 May 2009 02:41:46 GMT

ice !!!

would this work just as well (also ... you need the comma in the variable list for use in any of the CAT functions) ...

proc sql;
select name into :namelist separated by ','
from dictionary.columns
where libname="<libarary name>" and memname="<datasetname>" and name like "%_xxx";
quit;



Concatenating variables with same prefix or suffix

by snoopy369 » Sat, 30 May 2009 04:48:43 GMT

ops, yes, comma is useful.

I imagine like '%_xxx' would work just as well; not sure which is faster.
(Also, a substr with the length function is a third option; or a reverse
function and 'zyx_' instead of '_xyz') I tend not to use like because i've
been warned away from it in some circumstances due to speed concerns; but
i'm sure it's appropriate in some situations, I just conditioned myself not
to use it and ever since forget about it :)

-Joe

On Fri, May 29, 2009 at 1:41 PM, Mike Zdeb < XXXX@XXXXX.COM > wrote:



Similar Threads

1. Rounding and Prefixing and Suffixing Observations

2. Re How to remove variables with same suffix?

Kitty,

There is no easy answer, when you do not know enough SAS.  Here is
a basic solution which only requires SAS/BASE knowledge.

   %macro vspec ( lib=work, mem=t , spec=2002 2003, vlist=list, max=99) ;
       %* assign list of wanted variables to &vlist ;
       %* lazy way: ask user to separate libname from memname
                    and give upper bound on number of conditions
       ;
       %global &vlist ;
       %local i w _&vlist ;
       /* set up variable name requirements list */
       %do i = 1 %to &max ;
           %let w = %scan(&spec,&i) ;
           %if %length(&w) = 0 %then %goto endloop ;
           %let _&vlist = &&_&vlist or name like '%' || "&w" ;
       %end ;
       %endloop:
       %let _&vlist = %substr(&&_&vlist,4) ;

       /* extract list of variables */
       proc sql noprint ;
          select name into :&vlist separated by " "
             from dictionary.columns
             where libname = "%upcase(&lib)"
               and memname = "%upcase(&mem)"
               and ( &&_&vlist ) ;
       quit ;
   %mend  vspec ;

   /* test data */
   data w ;
      retain q1_2002 - q1_2020 q2_2002 - q2_2020 q3_2002 - q3_2020 7 ;
   run ;

   /* assign list */
   options mprint ;
   %vspec ( mem = w , spec = 2002 2003 )

   /* extract columns */
   options symbolgen ;
   data q ;
      set w ( keep = &list ) ;
   run ;
   options nosymbolgen ;

The good news is that this example includes most of the techniques needed
for manipulating lists of variables.

Ian Whitlock
=================
Date:         Mon, 24 Apr 2006 13:50:12 -0700
Reply-To:     Kitty Lee < XXXX@XXXXX.COM >
Sender:       "SAS(r) Discussion"
From:         Kitty Lee < XXXX@XXXXX.COM >
Subject:      How to remove variables with same suffix?
Content-Type: text/plain; charset=iso-8859-1
Hi All,
  Does anyone know how to remove the variables with the same suffix?  For
  example, if I have the following variables, Q1_2002, Q2_2002, Q3_2002,
  Q4_2002, Q1_2003, Q2_2003 etc...and if I want to keep only year 2003's
  and remove 2002's, what should I do?  I know with the same prefix I can
  do V:  (with a colon) but with the same suffix, I have no clues.
  Thank you!

---------------------------------
How low will we go? Check out Yahoo! Messenger low  PC-to-Phone call
rates.

3. How to remove variables with same suffix?

4. Help needed to Concatenate variables in SAS Dataset into a new variable

Hi

I have a SAS dataset with variables COL1, COL2 etc. The number of
these can vary.

I want to concatenate all of then into a new COL variable.

Can you help.

5. same operation on 100 variables with the same prefix var

6. reading variables with same prefix

Is there a difference in PROC IML when reading multiple variables with
the same prefix?  Is there an obvious solution to getting the
following to run:



proc iml;
use test_merge2;
read all var {act_wt}  into w;
read all var {fac_01-fac_68} into e;
wt=w`;
exp=wt*e;
print exp;
quit;

7. TRANSPOSE, creating variables with PREFIX= option

8. Removing prefixes in variable names