DATA Step, Macro, Functions and more

Using an array to convert numeric variables to character variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

Using an array to convert numeric variables to character variables

[ Edited ]

Why is this code not converting numeric variables to character variables? In fact, the log says it is doing the opposite, although a PROC CONTENTS reveals no conversion actually took place. Thank you for the assistance.

 

DATA a4_final_num;
	SET	a4_final_pre;
Array _numeric(*) QWB2A QWB2B QWB2C; Array _character(*) var1-var3; DO i=1 to dim(_numeric); _character(i) = PUT(_numeric(i),8.); END; RUN;

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 DATA a4_final_num;
57 SETa4_final_pre;
58
59 Array _numeric(*)
60 QWB2A
61 QWB2B
62 QWB2C;
63
64 Array _character(*) var1-var3;
65
66 DO i=1 to dim(_numeric);
67 _character(i) = PUT(_numeric(i),8.);
68 END;
69
70 RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
67:6


Accepted Solutions
Solution
‎03-15-2016 02:49 PM
Regular Contributor
Posts: 199

Re: Using an array to convert numeric variables to character variables

@Astounding Thank you. However, I tried that earlier w/ no effect. I just tried it again w/ your code. The PROC CONTENTS shows that they are still numeric: 

# Variable Type Len
351 QWB2A Num 8
352 QWB2B Num 8
353 QWB2C Num 8

 

DATA a4_final_num;
	SET	a4_final_pre;
	
 	Array _numeric(*) 
 	QWB2A
	QWB2B
	QWB2C;
	
	Array _character(*) $ 8 var1-var3;
  
  	DO i=1 to dim(_numeric);
     _character(i) = PUT(_numeric(i),8.); 
    END;
    
RUN;

 

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56
57 DATA a4_final_num;
58 SETa4_final_pre;
59
60 Array _numeric(*)
61 QWB2A
62 QWB2B
63 QWB2C;
64
65 Array _character(*) $ 8 var1-var3;
66
67 DO i=1 to dim(_numeric);
68 _character(i) = PUT(_numeric(i),8.);
69 END;
70
71 RUN;

NOTE: There were 404 observations read from the data set WORK.A4_FINAL_PRE.
NOTE: The data set WORK.A4_FINAL_NUM has 404 observations and 495 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds


72
73 PROC CONTENTS DATA=a4_final_num (KEEP=QWB2A
74 QWB2B
75 QWB2C);
76 RUN;

NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.10 seconds
cpu time 0.08 seconds


77
78 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
90

View solution in original post


All Replies
Super User
Posts: 5,084

Re: Using an array to convert numeric variables to character variables

By default, the second ARRAY statement defines VAR1-VAR3 as numeric.  You can control that:

 

array _character (3) $ 8 var1-var3;

Solution
‎03-15-2016 02:49 PM
Regular Contributor
Posts: 199

Re: Using an array to convert numeric variables to character variables

@Astounding Thank you. However, I tried that earlier w/ no effect. I just tried it again w/ your code. The PROC CONTENTS shows that they are still numeric: 

# Variable Type Len
351 QWB2A Num 8
352 QWB2B Num 8
353 QWB2C Num 8

 

DATA a4_final_num;
	SET	a4_final_pre;
	
 	Array _numeric(*) 
 	QWB2A
	QWB2B
	QWB2C;
	
	Array _character(*) $ 8 var1-var3;
  
  	DO i=1 to dim(_numeric);
     _character(i) = PUT(_numeric(i),8.); 
    END;
    
RUN;

 

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56
57 DATA a4_final_num;
58 SETa4_final_pre;
59
60 Array _numeric(*)
61 QWB2A
62 QWB2B
63 QWB2C;
64
65 Array _character(*) $ 8 var1-var3;
66
67 DO i=1 to dim(_numeric);
68 _character(i) = PUT(_numeric(i),8.);
69 END;
70
71 RUN;

NOTE: There were 404 observations read from the data set WORK.A4_FINAL_PRE.
NOTE: The data set WORK.A4_FINAL_NUM has 404 observations and 495 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds


72
73 PROC CONTENTS DATA=a4_final_num (KEEP=QWB2A
74 QWB2B
75 QWB2C);
76 RUN;

NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.10 seconds
cpu time 0.08 seconds


77
78 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
90

Super User
Posts: 5,084

Re: Using an array to convert numeric variables to character variables

Yes, that's true.  VAR1-VAR3 are now character where they used to be numeric.  But none of your code changes the existing variables, it only assigns values to new variables.

 

You could add this to your DATA step if your goal is to save the original variables (and using the original variable names) as character:

 

drop QWB2A QWB2B QWB2C;

rename var1=QWB2A var2=QWB2B var3=QWB2C;

 

It's a strange looking combination, but it does work.

 

Regular Contributor
Posts: 199

Re: Using an array to convert numeric variables to character variables

Thanks! That makes sense.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 481 views
  • 3 likes
  • 2 in conversation