DATA Step, Macro, Functions and more

concatenate all columns having same starting name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 117
Accepted Solution

concatenate all columns having same starting name

Hello,

 

I have a data set that holds 20 columns classe_1, classe_2, classe_3....classe_20.

possibles classe values : 5, 4, 17, C, B2.

i would like to concatenate all columns that begins with class_

thanks for your help in advance

regards

Nasser


Accepted Solutions
Solution
‎04-30-2018 02:54 AM
Super User
Posts: 13,321

Re: concatenate all columns having same starting name

Posted in reply to novinosrin

From the documentation cats and related concatenation functions will default to a length of 200 characters if not explicitly assigned prior to use. So if your 20 class variables have more than 10 characters each you may need to assign a maximum expected length.

 

Also if you want a space or comma between the classes then use Catx

 

var= catx(',', of class: );

or

var= catx(' ', of class: );

or (and especially if people are to read this result) a comma and a space

var= catx(', ', of class: );

View solution in original post


All Replies
PROC Star
Posts: 1,577

Re: concatenate all columns having same starting name

Posted in reply to Nasser_alfea
var=cats(of class_:);
Solution
‎04-30-2018 02:54 AM
Super User
Posts: 13,321

Re: concatenate all columns having same starting name

Posted in reply to novinosrin

From the documentation cats and related concatenation functions will default to a length of 200 characters if not explicitly assigned prior to use. So if your 20 class variables have more than 10 characters each you may need to assign a maximum expected length.

 

Also if you want a space or comma between the classes then use Catx

 

var= catx(',', of class: );

or

var= catx(' ', of class: );

or (and especially if people are to read this result) a comma and a space

var= catx(', ', of class: );

Frequent Contributor
Posts: 117

Re: concatenate all columns having same starting name

Posted in reply to novinosrin

hello novinosrin

 

thanks for your help. but I get this message "The CATS function call does not have enough arguments" by attempting your suggestion.

however it works with

var= catx('', of class: );

Super User
Posts: 6,631

Re: concatenate all columns having same starting name

[ Edited ]
Posted in reply to Nasser_alfea

That's because your actual variable names are classe_1, classe_2, etc., not class_1, class_2.  This would fix it (but would still have the issue of possibly removing too many blanks):

 

var = cats(of classe_: );

Contributor
Posts: 36

Re: concatenate all columns having same starting name

[ Edited ]
Posted in reply to Nasser_alfea

You can use this macro,  just add you DATASET NAME and LIBRARY NAME into it.

 

%macro getvars(lib,dsn);
    %global vlist;
    proc sql;
        select name into :vlist separated by ' '
        from dictionary.columns
        where memname = upcase("&dsn")
		and libname =upcase("&lib")
		and name like 'class%'
    ;quit;
%mend;

%getvars(LIBRARY,DATASET);

%Put &vlist
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 133 views
  • 3 likes
  • 5 in conversation