BookmarkSubscribeRSS Feed
lei
Obsidian | Level 7 lei
Obsidian | Level 7

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?

4 REPLIES 4
Astounding
PROC Star

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.

yashk
Fluorite | Level 6

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;


 

s_lassen
Meteorite | Level 14

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.

 

lei
Obsidian | Level 7 lei
Obsidian | Level 7

Thank you it works!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 1307 views
  • 0 likes
  • 4 in conversation