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 ;
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;
Thanks Reeza for your reply.Let me clarify myself. I want to avoid duplicated variables as well.
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.
many thanks for your reply but i need to avoid duplicated variables in new varlist.
Hi -
- order does not matter, although preferable
- yes variables in the list exist.
thx
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.
Hi - thanks for your reply.
- Capitalization does not matter.
- duplication in one list is not possible.
Thanks
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;
Astounding , Thanks soooooo much!!!!! It works very well .....
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.