I have two different character variables that are extracted from qualitative surveys (var7 and var8). I want to concatenate them together with a space in between. I wrote the cold below:
DATA NIC.DICTATION2;
SET NIC.DICTATION;
ARRAY OLD {*} VAR7-VAR8;
ARRAY NEW {*} $2000 NEW7-NEW8;
DO I=1 TO DIM(OLD);
NEW(I)=STRIP(PUT(OLD(I), $CHAR2000.));
VAR=(VAR||' '||NEW(I));
END;
WHERE VAR7^="";
RUN;
However, I keep on getting an error: NOTE: Invalid numeric data, ' wrist adduction' , at line 51 column 16.
NOTE: Invalid numeric data, ' abduction' , at line 51 column 16.
Can some one help?
To concatenate them together requires only a single statement after the SET statement:
newvar = catx(' ', var7, var8);
The program as it stands has several significant issues. But the one causing the message you see is the creation of VAR. What is VAR supposed to contain? Is it actually supposed to be a variable? It is mentioned on only one line of the program, and it is numeric. So the assignment of characters to VAR is causing this message.
Hello
try this, you are assigning character to numeric. This fixes it. I took some values as an example.
DATA x;
ARRAY OLD {*} VAR7-VAR8 (1,2);
ARRAY NEW {*} $2000 NEW7-NEW8 ('A','B');
DO I=1 TO DIM(OLD);
NEW(I)=STRIP(INPUT(OLD(I), $CHAR2000.));
VAR='VAR'!!' '!!NEW(I);
END;
RUN;
proc print data=x;
run;
Looks like you have some trouble with the way SAS handles character variables.
If I understand correctly you are trying to put the trimmed values of the old variables into the new variables. Which will not work, the STRIP and TRIM functions shorten their output, but when the variable values are put into the new variables, these are again padded to the right with blanks up to their length as declared (in this case 2000).
I think that what you want to do is something like this:
DATA NIC.DICTATION2;
SET NIC.DICTATION;
WHERE VAR7^="";
length var $200; /* or whatever length is enough */
var=strip(var7)!!' '!!strip(var8);
RUN;
Which is much easier to do with the CATX function:
Data Nic.Dictation2;
set Nic.Dictation;
Where Var7 ^= ' ';
var=catx(' ',of var7-var8);
run;
The CATX function automatically assigns a length of 200 to the result, if the output variables is not previously declared. You can get another length by using a length statement first. As shown, you can use OF to use a list of variables as parameters (as you are using arrays, my guess is that you actually have more variables than two).
And you will get programs that are much easier to read if you stop putting everything in CAPS.
Thank you it works!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.