BookmarkSubscribeRSS Feed
wateas
Calcite | Level 5

I'm stumped as to why the concatenate function "cat" is not working for me.

 

data df;
	set df;
	Trt_Unique = cats(Trt_Amend, "_", Trt_App, "_", Trt_CC);
run;

20240627 - SAS concat error.png

 

I don't know what to make of the error indicating that argument 5 is "invalid".  The string "no_CC" concatenates just fine to other strings in the variable.  Notice how "Ctrl-Fert_NA_CC" concatenates just fine, but there is an error for "Ctrl-Fert_NA_no_CC".  Printing the data shows that these values are omitted by the operation.

 

20240627 - SAS concat error 2.png

 

The issue doesn't seem to be that the character limit is exceeded.  Using a length statement prior to the set statement causes the variable to not be created for some odd reason, without any errors to indicate why.  Any ideas regarding this issue would be appreciated.  Thanks for reading.

6 REPLIES 6
Tom
Super User Tom
Super User

I would suggest re-typing the line.  Most likely there is some strange invisible character there outside of the quotes and  that is what is causing the issue.

Kurt_Bremser
Super User

Select the rows where the CATS fails, and display the trt_cc variable with a $HEX format (use double the defined length of the variable) to see if there's some "funny stuff" hidden.

If this does not reveal anything, open a track with SAS technical support.

wateas
Calcite | Level 5

I don't see any unusual characters when examining the applicable data in the source CSV file.

 

Can you provide help with displaying the data in the HEX format?  I tried the code below, which only deleted the data set.

 

data df;
   put Trt_CC $hex10.;
run;
ballardw
Super User

After you rebuild your data set then use

data _null_; 
   set df;
   put Trt_CC $hex10.;
run;

 The DATA statement names the output data set. When you do not provide any input, read a file, inline data or other data sets on a SET, MERGE, MODIFY or UPDATE statement then you have told SAS to create an empty data set. The DATA _NULL_; means do not create a data set for output. BUT you can do anything with data provided to the step, such as put the variables.

 

Note: For beginners (and many moderately experienced SAS users) having the input data set and the output the same is dangerous as you can accidentally corrupt or change data.

 

Consider: You need to change x by subtracting 1 from the values and write:

data df;
   set df;
   x= x-1;
run;

Then realize that you needed to make another change to a different variable and add a statement to the code:

data df;
   set df;
   x= x-1;
   y = log(someothervariable);
run;

After you run the above code X has had the 'subtract 1' applied twice. A few more minor coding changes and you can end up with x as having the 1 subtracted multiple times.

 

I inherited a data set that had a variable that was supposed to have values of 1, 2, and 3. But all the values were 1 because code like this had been run multiple times to "recode" the original values:

data old;
   set old;
   if x=4 then x=3;
  else if x=3 then x=2;
  else if x=2 then x=1;
run;

This was supposed to shift a variable from 1,2,3,4 to 1,2,3. And does so. The FIRST time the code runs.

But other recode bits were added to the same data step. So the code was run against the first already changed values. So what had been a 4 and become a 3 then changed to 2. Then run the code again and it was now 1. As were all the values.

 

Make a new data set:

data df_new;
   set df;
   /* other code goes here*/
run;

Then when you make LOGIC mistake, not a syntax mistake, then you still have your DF set to work with.

Murphy's Law in this case that your logic mistake will never involve a syntax error and will run corrupting your data.

Tom
Super User Tom
Super User

You can get that NOTE when the value generated by CATS() is too long for the target variable.  But normally you also get another more descriptive WARNING message .

1    data test;
2      length x $5 y $10;
3      x='12345';
4      y=cats(x,x,x,x,x);
5    run;

WARNING: In a call to the CATS function, the buffer allocated for the result was not long enough to contain the concatenation of
         all the arguments. The correct result would contain 25 characters, but the actual result might either be truncated to 10
         character(s) or be completely blank, depending on the calling environment. The following note indicates the left-most
         argument that caused truncation.
NOTE: Argument 3 to function CATS('12345','12345','12345','12345','12345') at line 4 column 5 is invalid.
x=12345 y=  _ERROR_=1 _N_=1
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 629 views
  • 4 likes
  • 4 in conversation