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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 581 views
  • 2 likes
  • 3 in conversation