BookmarkSubscribeRSS Feed
TimurShangareev
Calcite | Level 5

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?

1 REPLY 1
Tom
Super User Tom
Super User

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

Tom_0-1759251974576.png

 

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;

Tom_0-1759252797817.png

 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 204 views
  • 0 likes
  • 2 in conversation