BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hello,

I would like to change some variables from numeric to character.  I use array function, but I found some error messages in the log window.  Please help.

data cost_demagrapy_2;
	set cost_demagrapy;
	array var {*} Sex Age Insurance race;                                                                                                    
     do i=1 to dim(var);                                                                                                                
      	rename newvar=var(i);
		var_=put(newvar(i),10.);
		drop var(i); 
     output;                                                                                                                             
     end;   
run; 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So your code has a major logic error that the SAS compiler cannot detect.

But it also have two syntax errors that it can detect.

If you want to use parentheses in a variable name you need set the option VALIDVARNAME to ANY and then use a name literal in your code.

rename newvar='var(i)'n;

If you want to reference an element in an array you need to first define the array.  You never defined any NEWVAR as an array.

 

Perhaps you meant to do something like:

data cost_demagrapy_2;
  set cost_demagrapy;
  array OLD Sex Age Insurance race;
  array NEW $10 _Sex _Age _Insurance _race;
  do index=1 to dim(old);
    new[index]=put(old[index],10.);
  end;
  rename _Sex=Sex _Age=Age _Insurance=Insurance _race=race;
  drop Sex Age Insurance race index;
run;

 

View solution in original post

6 REPLIES 6
ybz12003
Rhodochrosite | Level 12
545 data cost_demagrapy_2;
546 set cost_demagrapy;
547 array var {*} Sex Age Insurance race;
548 do i=1 to dim(var);
549 rename newvar=var(i);
-
22
76
550 var_=put(newvar(i),10.);
------
68
ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 68-185: The function NEWVAR is unknown, or cannot be accessed.

551 drop var(i);
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.

ERROR 76-322: Syntax error, statement will be ignored.

552 output;
553 end;
554 run;

Tom
Super User Tom
Super User

So your code has a major logic error that the SAS compiler cannot detect.

But it also have two syntax errors that it can detect.

If you want to use parentheses in a variable name you need set the option VALIDVARNAME to ANY and then use a name literal in your code.

rename newvar='var(i)'n;

If you want to reference an element in an array you need to first define the array.  You never defined any NEWVAR as an array.

 

Perhaps you meant to do something like:

data cost_demagrapy_2;
  set cost_demagrapy;
  array OLD Sex Age Insurance race;
  array NEW $10 _Sex _Age _Insurance _race;
  do index=1 to dim(old);
    new[index]=put(old[index],10.);
  end;
  rename _Sex=Sex _Age=Age _Insurance=Insurance _race=race;
  drop Sex Age Insurance race index;
run;

 

ballardw
Super User

Are you really trying to rename variables to the value of a different variable????

 

Not in rename statement. Rename is not executable. The Rename statement (or data set option) requires you to specify the name as text. You cannot use the value of a variable on the rename statement.

The data step basically doesn't allow you to use functions to build variable names at all.

 

Your partial statement "some variables from numeric to character. I use array function" just will not work. Variables in an array must be all of the same type, either all numeric or all character.

 

You can get the name of a variable referenced in an array with the VNAME such as where Var[i] is a legal array reference:

Thevariablenameis = vname(var[i]);

 

As we very frequently ask: provide an example of the data in data step form and the desired result.

Reeza
Super User

I don't think you can use rename in that fashion. 

You also seem to be referencing an array newvar(i) but I don't see any such array declared.

 

ybz12003
Rhodochrosite | Level 12
I don't really need to rename the variables. My main purpose is to change numeric to chatater.
Reeza
Super User

@ybz12003 wrote:
I don't really need to rename the variables. My main purpose is to change numeric to chatater.

SAS doesn't allow conversion within the same variable name so you need new variable names. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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