Using SAS 9.4
I am running the following code:
data want;
input a $ b $ c $;
datalines;
one two three
one two three
one two three
;
run;
data check2(drop=i);
set want;
length mme $ 500;
array tots _character_;
do i = 1 to dim(tots);
if tots[i] ne '' then do;
mme = catx(',', mme, coalescec(tots[i]));
end;
end;
run;
I have 3 character variables that I am trying to combine all possible combinations and separate with a ",". However, when I run this code it repeats the combination twice. Is there a way to get the data to not repeat the second time? Thank you
Put the
length mme $ 500;
statement AFTER the
array tots _character_;
statement
Otherwise the array includes MME, explaining your unexpected results.
The compiler interprets _character_ and _numeric_ (and _all_) based on the variables it knows about at the point in the code that they appear.
Put the
length mme $ 500;
statement AFTER the
array tots _character_;
statement
Otherwise the array includes MME, explaining your unexpected results.
The compiler interprets _character_ and _numeric_ (and _all_) based on the variables it knows about at the point in the code that they appear.
The problem is that when you say
array tots _character_;
variable mme is already defined and thus is included in the list of _character_ variables referenced by array tots. So the simple solution is to define the array sooner:
data check2(drop=i);
set want;
array tots _character_;
length mme $ 500;
do i = 1 to dim(tots);
if tots[i] ne '' then do;
mme = catx(',', mme, coalescec(tots[i]));
end;
end;
run;
Note that you could also do:
data check3(drop=i);
set want;
array tots _character_;
length mme $ 500;
mme = catx(",", of tots[*]);
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.