BookmarkSubscribeRSS Feed
Yul
Obsidian | Level 7 Yul
Obsidian | Level 7

hello,

How can I convert multiple string variables to numeric?

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You can use ARRAY in a DATA step.

 

--
Paige Miller
Yul
Obsidian | Level 7 Yul
Obsidian | Level 7

hi,

If I have MULTIPLE string variables, some of them include data and some empty but I still need them in the new data as numric.

how should i do it?

 

for example - variables: xxx  yyy zzz ggg jjj hhh 

 

PaigeMiller
Diamond | Level 26

Please provide a small example data set, following these instructions (and not via any other method)

https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/

--
Paige Miller
Hagay
SAS Employee

שלום,

 

אולי הדוגמא הזאת תעזור:

* Generating a sample dataset;
data MY_CLASS;
	set SASHELP.CLASS;
	length 
		My_Age	$8
		My_Height	$10;
	My_Age=strip(put(age, 32.));
	if sex='M' then My_Height=strip(put(height*2.54, 10.3));
	else My_Height='';
run;

option mprint; * To see the SAS code that the following macro generates;
* Converting strings to numbers; %let my_vars_list=My_Age My_Height; * The variable list - use space as a separator; %macro convert_to_nums; %let my_vars_count=%sysfunc(countw(&my_vars_list)); * The number of variables in our list; %put &=my_vars_count; data MY_CLASS_NUM; set MY_CLASS; length %do i=1 %to &my_vars_count; %scan(&my_vars_list, &i)_Num 8 %end; ; %do i=1 %to &my_vars_count; %scan(&my_vars_list, &i)_Num = input(%scan(&my_vars_list, &i), best.); %end; run; %mend; %convert_to_nums; * BONUS - you can use SAS code to generate the variables list; * For example: All the string variables that their name starts with "my_"; proc sql noprint; select name into :my_vars_list separated by " " from DICTIONARY.COLUMNS where libname='WORK' and memname='MY_CLASS' and type='char' and lowcase(name) like 'my_%'; quit; %put &=my_vars_list;

עורכי הדין שלי ביקשו גם לציין ש: "הקוד לעיל מובא להמחשה בלבד. כל שימוש בו הוא על אחריות המשתמש בלבד." 😁

 

חגי

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Discussion stats
  • 4 replies
  • 1247 views
  • 0 likes
  • 3 in conversation