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):(Column).
67:6
@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
By default, the second ARRAY statement defines VAR1-VAR3 as numeric. You can control that:
array _character (3) $ 8 var1-var3;
@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
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.