New Contributor
Posts: 2

# Converting numerical to character (dummy variable)

Hello, in my statistics university-course we have a group assignment where we are supposed to do a regression analysis, a predictive model for wage.
We are given a dataset with 2670 ish observations with the variables Wage, Education, Experience etc, One of the variables is flexibility, the 'observation' (=person being interviewed) is coded as 0 for No and 1 for Yes, when asked the question:

"Do you have any type of flexible working hours, ie You can, within certain limits decide when you start and finish work? "

The problem I have is that I, after hours of me+my assignmentgroup-buddies googling it but finding no concrete answer (that we can understand), don't know how to code the data which is numerical, to character variables (dummy variables)

Does anyone know how to solve our problem?

Super User
Posts: 10,287

## Re: Converting numerical to character (dummy variable)

If you want the new variables to contain yes or no, use a defined format:

(create the format)

proc format library=work;

value yesno

0='N'

1='Y'

other='*'

;

run;

then do

newvar = put(oldvar,yesno.);

in a data step.

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

## Re: Converting numerical to character (dummy variable)

Dear KurtBremser,

How to write code for second step of your comment?

<then do

newvar = put(oldvar,yesno.);

in a data step.>

As I am new with sas and my data was import from Spss.

Thank you.

Super User
Posts: 9,617

## Re: Converting numerical to character (dummy variable)

proc format;

value yn

1 = "Yes"

0 = "No";

run;

data yourdata;

set yourdata;

format yourvariable yn.;

run;

I don't like formats however, so:

data yourdata;

set yourdata;

select(yourvariable);

when(1) decoded_variable="Yes";

when(0) decoded_variable="No";

otherwise decoded_variable=strip(put(yourvariable,best.));

end;

run;

You can also do an SQL case when, or merge the coded value in from a dataset, or create a sub-query with a code list table which is my preferred method.  So have a dataset for coded values then:

proc sql;

create table WANT as

select     A.*,

(select THIS.LONG_VALUE from DECODE_DATASET THIS where THIS.CODE=A.CODE) as LONG_VALUE

from       HAVE A;

quit;

Discussion stats
• 3 replies
• 1258 views
• 6 likes
• 4 in conversation