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

Hello. I'm trying to convert multiple character variables to numeric variables.

 

My data are as follows....

 

ID pre_stress_visit1  pre_stress_visit2 pre_stress_visit3 post_stress_visit1 post_stress_visit2 post_stress_visit3 pre_pain_visit1

A 12 10 9 10 8 7 53 ...

B 13 13 6 11 11 4 57 ...

C 13 12 8 10 13 7 55 ...

 

the pre_stress_visitnumber and post_stress_visitnumber are recognized as character. So, i want to convert them into numeric variables

 

Though I can convert each variables with input statement, like 'input(pre_stress_visit1, best32.)', it takes long time to do it. Is there a better way to convert them?

 

I would really appreciate all your help. thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@km0927 

You can always generate the code if you believe that's a better option than just explicitly writing the 60 statements.

data have;
  array chars {*} $15. id pre_stress_visit1  pre_stress_visit2 pre_stress_visit3 post_stress_visit1 post_stress_visit2 post_stress_visit3 pre_pain_visit1 (8*'25');
  output;
  stop;
run;

proc sql noprint;
  select 
    cats(name,'=','_',substrn(name,1,31))
    ,cats('_',substrn(name,1,31))
    ,cats(name,'=','input(_',substrn(name,1,31),',best32.);')
      into 
        :rename_list separated by ' '
        ,:drop_list separated by ' '
        ,:assign_list separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE' and type='char' and upcase(name) not in ('ID')
  ;
quit;

data want;
  set have(
    rename=(&rename_list)
    );
  drop &drop_list;
  &assign_list;
run;

 

Question is: Why do you end-up with all character in first place? Is reading the data into numerical variables from the beginning something you could influence? 

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

> Though I can convert each variables with input statement, like 'input(pre_stress_visit1, best32.)', it takes long time to do it. Is there a better way to convert them?

 

That's the best way to convert them, and I doesn't take long at all.

What makes you think it takes long?

 

km0927
Obsidian | Level 7

In fact, this is an example and i have more about 50-60 more variables to deal with.

 

If there is no other way, i will try to cope with it.

Patrick
Opal | Level 21

@km0927 

You can always generate the code if you believe that's a better option than just explicitly writing the 60 statements.

data have;
  array chars {*} $15. id pre_stress_visit1  pre_stress_visit2 pre_stress_visit3 post_stress_visit1 post_stress_visit2 post_stress_visit3 pre_pain_visit1 (8*'25');
  output;
  stop;
run;

proc sql noprint;
  select 
    cats(name,'=','_',substrn(name,1,31))
    ,cats('_',substrn(name,1,31))
    ,cats(name,'=','input(_',substrn(name,1,31),',best32.);')
      into 
        :rename_list separated by ' '
        ,:drop_list separated by ' '
        ,:assign_list separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE' and type='char' and upcase(name) not in ('ID')
  ;
quit;

data want;
  set have(
    rename=(&rename_list)
    );
  drop &drop_list;
  &assign_list;
run;

 

Question is: Why do you end-up with all character in first place? Is reading the data into numerical variables from the beginning something you could influence? 

Kurt_Bremser
Super User

"are recognized as character"

 

Why?

 

If you use proc import to get data into SAS, replace it with a more robust method and use a data step that you write to read the data.

Kurt_Bremser
Super User

You also suffer from a VERY BAD data model (wide).

If you transpose this mostly unusable structure to the much better long format, you probably end up with just two variables that need conversion.

 

See Maxims 33, 19, 31, 22 (and probably some others)

km0927
Obsidian | Level 7

Thanks for letting me know the importance of 'proc import' format.

 

I will take advice and transform it into another format.

data_null__
Jade | Level 19

@km0927 wrote:

Hello. I'm trying to convert multiple character variables to numeric variables.

 

My data are as follows....

 

ID pre_stress_visit1  pre_stress_visit2 pre_stress_visit3 post_stress_visit1 post_stress_visit2 post_stress_visit3 pre_pain_visit1

A 12 10 9 10 8 7 53 ...

B 13 13 6 11 11 4 57 ...

C 13 12 8 10 13 7 55 ...

 

the pre_stress_visitnumber and post_stress_visitnumber are recognized as character. So, i want to convert them into numeric variables

 

Though I can convert each variables with input statement, like 'input(pre_stress_visit1, best32.)', it takes long time to do it. Is there a better way to convert them?

 

I would really appreciate all your help. thanks.


PROC TRANSPOSE has features that make this easy and you can keep the variable labels.

 

data prepost;
   infile cards missover;
   input ID:$1.  (pre_stress_visit1 pre_stress_visit2 pre_stress_visit3 post_stress_visit1 post_stress_visit2 post_stress_visit3 pre_pain_visit1)($);
   attrib _all_ label='Test label';
   cards;
A 12 10 9 10 8 7 53 ...
B 13 13 6 11 11 4 57 ...
C 13 12 8 10 13 7 55 ...
;;;;
   run;
proc transpose data=prepost out=tall;
   by id;
   var p:;
   run;
proc print;
   run;
data tall;
   set tall;
   num1 = input(col1,f9.);
   run;
proc transpose data=tall out=prepostnum(drop=_name_);
   by id;
   var num1;
   run;
proc contents varnum;
proc print;
   run;

Capture.PNG

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 3045 views
  • 3 likes
  • 5 in conversation