I have two variables, which contains list of dataset variables. A = "VAR1 VAR2 VAR4" and B = "VAR3 VAR4". I want to get string with all 4 VAR1-4 (ascending order is not required). Would be better to solve it inside one Datastep. Is it possible? Or I have to lists VARNAME functions for each of varnum based on NVARS attribute applied to "DSNAME(keep=" || A " " || "B" || ")"? Or I have to use 2 arrays and loops? Is there a better/elegant solution?
An array would only be useful if you have multiple variables you wanted to reference.
But that is not what I took your statements to mean. Instead if sounds like you have just TWO variables, both of which have space delimited lists of words.
data have;
infile cards dsd truncover ;
input (a b) (:$50.);
cards;
VAR1 VAR2 VAR4,VAR3 VAR4
x y z,a X c
;
So make a new variable (or just update one of the existing ones).
data want;
set have;
length all $100 ;
all=a;
do i=1 to countw(b,' ');
if 0=findw(all,scan(b,i,' '),' ','i') then all=catx(' ',all,scan(b,i,' '));
end;
drop i;
run;
Result
But it would be simpler if your data structure just had one word per observation.
data have;
input type :$10. name :$32. ;
cards;
a VAR1
a VAR2
a VAR4
b VAR3
b VAR4
;
proc sql;
create table want as
select distinct name from have
;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.