BookmarkSubscribeRSS Feed
Asquared
Calcite | Level 5

 

In my original dataset, I have gender mentioned as Male and Female and therefore i'm unable to use PROC MEANS or UNIVARIATE. When i converted female=0 and male= 1 in the original data set, i was able to run these procedures. But it's  cumbersome process since i have 90 other such variables that are in character form.

 

Two questions:

 

1. How can i convert charac into numeric form in SAS

 

If i have to convert zones, for example- north, south, east and west to 0,1,2 and 3 respectively, what will the code look like?

 

 

2. Is there a way by which i can run Means or univariate or other such commands with the charac variables?

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

What are you trying to do with this categorical variable? You use proc univariate for a continuous numeric variable. With a categorical variable such as gender/sec, use proc freq

 

proc freq data = sashelp.class;
	tables sex;
run;

Examples are given in the documentation here

 

 

https://support.sas.com/documentation/cdl/en/procstat/63104/HTML/default/viewer.htm#procstat_freq_se...

Kurt_Bremser
Super User

If you need to convert a categorical variable to numeric (limited cardinality!), use formats:

data have;
input zone $;
cards;
north
south
west
east
west
north
;
run;

%let varname=zone;

proc sort
  data=have (
    keep=&varname
    rename=(&varname=start)
  )
  out=cntlin
  nodupkey
;
by start;
run;

data cntlin;
set cntlin;
fmtname = "_&varname._char";
type = 'I';
label = _n_;
run;

data cntlin2;
set cntlin (rename=(label=start start=label));
fmtname = "_&varname._num";
type = "N";
run;

proc format library=work cntlin=cntlin;
run;

proc format library=work cntlin=cntlin2;
run;

data want;
set have;
_&varname._num = input(&varname,_&varname._char.);
format _&varname._num _&varname._num.;
run;

proc sql;
/* just to show that the new variable really is numeric,
   although it displays as character values */
select sum(_&varname._num) from want;
quit;

Note that you only have to set the variable name once, so you can easily use this procedure repeatedly (call execute) from a dataset that contains your variable names (retrieve from dictionary.columns/sashelp.vcolumn).

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is in relation to your other question regarding importing data from Excel isn't it.  You could save yourself a whole lot of effort by removing the two obstacles in your path - 1) Excel, which is an unstructured file format, 2) Proc import, which is a guessing procedure.  These two combined will make your data garbage.  I would advise:

1) Save to CSV file format, this is a text based file format.  Save as from Excel.

2) Take the proc import code, or write it yourself, a datastep import where you specify how each data element should be read in.  It would look something like:

data want;
  infile "your_csv_file.csv" dlm=",";
  length ...;
  informat ...;
  format ...;
  input ...;
run;

Obviously the ... would be replaced by the lists of variables to be read in.  In this way you can a) fix the data as it comes into SAS into character/numeric as you know the data to be, apply formatting - such as Genders, and do any other process, all in one simple step avoiding Excel "features" and proc import trying to guess what you want to do.

 

As a final tip, if this is Pharma data, then perhaps look at the CDISC models, by mapping to those you would put values and codes in the dataset so you would have everything ready for analysis.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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