I have numeric variables with character values, SAS9

Reply
New Contributor
Posts: 3

I have numeric variables with character values, SAS9

Hello, I am a fairly new SAS user and I am facing an issue with my code and/or the dataset I am working with.

 

When running a proc contents I have 5 variables that have a Type of Numeric, but the values taken on by these 5 variables are either Present, Absent, or . (missing). When I run the below code I get a clean log with no errors or warning messages; although all 8 variables output I only get one observation with all missing values ( . )

 

data redcap3;
     array LN(*) acr_ii acr_iii acr_iv acr_v;
     array Class(*) $ class_ii class_iii class_iv class_v;
     do i = 1 to dim(LN);
               class(i) = put(LN(i), $8.);
          end;
     drop i;
proc print;
run;

 

Does anyone know how to work with such variables? What steps do I need to take to change the variable type to character / or even code them as binary variables?

 

Any help is greatly appreciated!

 

 

Super User
Posts: 10,209

Re: I have numeric variables with character values, SAS9

Posted in reply to sassafras_

Look at the formats assigned to the variables.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 3

Re: I have numeric variables with character values, SAS9

Posted in reply to KurtBremser

Can you please elaborate? Where would I go from there? 

 

 

Super User
Posts: 23,663

Re: I have numeric variables with character values, SAS9

Posted in reply to sassafras_

You likely have a format attached to the data. 

Run a PROC CONTENTS and examine the output, specifically the type and format for your field of interest.

 

A format allows data to be numeric underneath, but it can be viewed with the description. 

For example if you had a data set with Age, 1, 2,3 etc but wanted to see the numbers as One, Two, Three, you could use a format. 

 

In the example below, I've created another age variable, identical the age value, but applied a format. 

 

data class;
set sashelp.class;

age_numeric_format = age;
age_character = put(age, words.);

format age_numeric_format words.;
run;

title 'Demo of Formats';
proc print data=class;
run;

title 'Check formats and type for AGE variables';
proc contents data=class;run;
title;

You can remove a format from a variable to see the underlying values using a FORMAT statement:

data class_remove_formats;
set class;

format age_numeric_format ;*removes formats from age;
run;

title 'No format on age numeric format variable';
proc print data=class_Remove_formats;
run;
title;

Removing the formats is likely all you need/want to do.

 

format class_ii class_iii class_iv class_v;

@sassafras_ wrote:

Hello, I am a fairly new SAS user and I am facing an issue with my code and/or the dataset I am working with.

 

When running a proc contents I have 5 variables that have a Type of Numeric, but the values taken on by these 5 variables are either Present, Absent, or . (missing). When I run the below code I get a clean log with no errors or warning messages; although all 8 variables output I only get one observation with all missing values ( . )

 

data redcap3;
     array LN(*) acr_ii acr_iii acr_iv acr_v;
     array Class(*) $ class_ii class_iii class_iv class_v;
     do i = 1 to dim(LN);
               class(i) = put(LN(i), $8.);
          end;
     drop i;
proc print;
run;

 

Does anyone know how to work with such variables? What steps do I need to take to change the variable type to character / or even code them as binary variables?

 

Any help is greatly appreciated!

 

 


 

New Contributor
Posts: 3

Re: I have numeric variables with character values, SAS9

Thank you so much for the constructive feedback! I didn't realize it was so simple to remove formats.

 

This worked and I am able to move forward now. I greatly appreciate your help.

Ask a Question
Discussion stats
  • 4 replies
  • 114 views
  • 1 like
  • 3 in conversation