BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PGStats
Opal | Level 21

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;
PG

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

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
  • 2 replies
  • 819 views
  • 1 like
  • 3 in conversation