Help using Base SAS procedures

Converting numerical to character (dummy variable)

Reply
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?
Thanks in advance!

Super User
Posts: 6,936

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
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
Super User
Posts: 7,401

Re: Converting numerical to character (dummy variable)

Well, take your pick:

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;

Ask a Question
Discussion stats
  • 3 replies
  • 725 views
  • 6 likes
  • 4 in conversation