BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_maldini_
Barite | Level 11

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

1 ACCEPTED SOLUTION

Accepted Solutions
_maldini_
Barite | Level 11

@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

4 REPLIES 4
Astounding
PROC Star

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

 

array _character (3) $ 8 var1-var3;

_maldini_
Barite | Level 11

@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

Astounding
PROC Star

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.

 

_maldini_
Barite | Level 11
Thanks! That makes sense.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 6904 views
  • 3 likes
  • 2 in conversation