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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.