BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

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 ;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

10 REPLIES 10
Reeza
Super User
%let clist = &alist. &blist.;
Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

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

FreelanceReinh
Jade | Level 19

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.

 

Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

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

data_null__
Jade | Level 19
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?
Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

Hi - 

- order does not matter, although preferable 

- yes variables in the list exist. 

 

thx

Astounding
PROC Star

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.

Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

Hi - thanks for your reply.

 

- Capitalization does not matter.

- duplication in one list is not possible.

Thanks

Astounding
PROC Star

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;

Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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