BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hi, this is a data manipulation exercise. Could you tell me what's wrong with my code for turning numeric variables into character using the following code?

 

i keep getting messages about undeclared array reference

data have; 
infile datalines dsd dlm=",";
	input id $ var1 var2 var3;
datalines;
a, 34, 45, 12
b, 41, 92, 14
c, 92, 29, 11
;
run;

data have1; set have;
	array var[3] _numeric_;
		do i=1 to dim[_numeric_];
			new[i]=strip(put(var[i], best.));
		end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data have; 
infile datalines dsd dlm=",";
	input id $ var1 var2 var3;
datalines;
a, 34, 45, 12
b, 41, 92, 14
c, 92, 29, 11
;
run;

data want;
set have;
array _old(*) var1-var3;
array _new(*) new_var1-new_var3;

do i=1 to dim(_old);
    _new(i) = put(_old(i), best. -l);
end;

drop i;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

You have not declared an array named NEW. So you can't assign values to it.

 

Also, you declare an array named VAR. Later you refer to an array as _VAR, not the same thing. So SAS doesn't like that either.

--
Paige Miller
Hello_there
Lapis Lazuli | Level 10

Could i please receive some help on how to do this?
I'm hitting a brick wall because usually when i do arrays i just use the same variable name as the ones i'm changing, but bc i can't use the same variable name because i'm converting numeric to characer, i don't understand what the solution is.

 

edit: i changed _var to var in the OP

Reeza
Super User
You can't use the method you have there. How will you know what the names of the new variables will be? They cannot be the same names? So your new array needs to have the names explicitly assigned somehow.
Hello_there
Lapis Lazuli | Level 10
Hi Reeza, so i guess it's not possible to do it more efficiently?

I guess it would be better just to do it
the long way
new1=strip(put(var1));
....
new3=strip(put(var3));
Reeza
Super User
data have; 
infile datalines dsd dlm=",";
	input id $ var1 var2 var3;
datalines;
a, 34, 45, 12
b, 41, 92, 14
c, 92, 29, 11
;
run;

data want;
set have;
array _old(*) var1-var3;
array _new(*) new_var1-new_var3;

do i=1 to dim(_old);
    _new(i) = put(_old(i), best. -l);
end;

drop i;
run;
Hello_there
Lapis Lazuli | Level 10
Thanks, Reeza!

What is the purpose of " -l" in the piece of code below?

_new(i) = put(_old(i), best. -l);
Reeza
Super User
left alignment for the character variable to avoid weird spacing issues.

See the documentation (PUT) for the other letters/alignments.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 7 replies
  • 925 views
  • 2 likes
  • 3 in conversation