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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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