SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to merge two varlists

Accepted Solution Solved
Reply
Contributor Moh
Contributor
Posts: 20
Accepted Solution

How to merge two varlists

How to merge Alist and Blist to have Clist containing all variables in two mentioned lists?

%let Alist = x1 x2 x3 x4 x5 ;

%let Blist = x4 x5 x6 ;

 


Accepted Solutions
Solution
‎01-09-2016 11:25 AM
Super User
Posts: 5,083

Re: How to merge two varlists

Here's an approach ... you would need to apply it to both ALIST and BLIST.  You would clearly need to encapsulate this inside a macro definition as well since it uses %DO and %IF.

 

%let clist=%upcase(&alist);

 

%do i=1 %to %sysfunc(countw(&blist));

   %let onevar = %upcase(%scan(&blist, &i));

   %if %index(%str( &clist ), %str( &onevar ))=0 %then %let clist = &clist &onevar;

%end;

View solution in original post


All Replies
Super User
Posts: 17,829

Re: How to merge two varlists

%let clist = &alist. &blist.;
Contributor Moh
Contributor
Posts: 20

Re: How to merge two varlists

Thanks Reeza for your reply.Let me clarify myself. I want to avoid duplicated variables as well.

Trusted Advisor
Posts: 1,115

Re: How to merge two varlists

I guess you wrote "merge" and not "concatenate" because you would like your "Clist" to be free of duplicates.

 

I'm not aware of an easy solution to this task, but a quick search on the web brought up this discussion:

http://stackoverflow.com/questions/33640976/how-do-i-deduplicate-words-in-a-character-variable

 

The solution provided by user "Chris J" promises to deduplicate the concatenated list suggested by @Reeza, but I haven't tested it.

 

However, it depends on how you are going to use Clist, whether you really need to have it duplicate free. In DROP or KEEP statements (or dataset options), for example, duplicate variable names do no harm.

 

Contributor Moh
Contributor
Posts: 20

Re: How to merge two varlists

many thanks for your reply but i need to avoid duplicated variables in new varlist. 

Respected Advisor
Posts: 3,777

Re: How to merge two varlists

Do you need to maintain any particular order of the variable names in the list? Do the variables mentioned in the lists exist, or need to exist, or are they just arbitrary SAS-names?
Contributor Moh
Contributor
Posts: 20

Re: How to merge two varlists

Hi - 

- order does not matter, although preferable 

- yes variables in the list exist. 

 

thx

Super User
Posts: 5,083

Re: How to merge two varlists

A few more considerations ...

 

Does capitalization matter?  If one list contains "name" and the other contains "Name", are those duplicates?

 

Could duplicates exist within just one of the lists?  For example, is it possible that &alist = name height name?

 

I know I have seen this problem addressed in a SAS Press book.  Macro Language Magic has a chapter on generating text.  But I would hesitate to just copy a section of copyrighted material here.

 

Good luck.

Contributor Moh
Contributor
Posts: 20

Re: How to merge two varlists

Hi - thanks for your reply.

 

- Capitalization does not matter.

- duplication in one list is not possible.

Thanks

Solution
‎01-09-2016 11:25 AM
Super User
Posts: 5,083

Re: How to merge two varlists

Here's an approach ... you would need to apply it to both ALIST and BLIST.  You would clearly need to encapsulate this inside a macro definition as well since it uses %DO and %IF.

 

%let clist=%upcase(&alist);

 

%do i=1 %to %sysfunc(countw(&blist));

   %let onevar = %upcase(%scan(&blist, &i));

   %if %index(%str( &clist ), %str( &onevar ))=0 %then %let clist = &clist &onevar;

%end;

Contributor Moh
Contributor
Posts: 20

Re: How to merge two varlists

Thanks soooooo much!!!!! It works very well .....

☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 555 views
  • 2 likes
  • 5 in conversation