Programming the statistical procedures from SAS

replacing contents of a variable

Reply
Frequent Contributor
Posts: 117

replacing contents of a variable

A simple question

I have a dataset containing a variable 'Sex', with values 1 and 2 (this is a numerical variable)

I want to change the contents of the variable to Female if Sex=1 and Male if Sex =2.

So the type of the variable will need to be changed too

Any simple way of doing it (without creating a temporary dummy variable?)

Best wishes

Super Contributor
Posts: 490

Re: replacing contents of a variable

It is the way mentioned in sas doc

Sample 24590: Convert values from character to numeric or from numeric to character

may i ask what is the problem with this way?

You can do the convert also in PROC SQL

check this old thread

Super Contributor
Posts: 251

Re: replacing contents of a variable

Not sure of your question.

Do you want a solution like this?

proc format;

value num2ch 1 = 'Female'

             2 = 'Male'

             ;

run;

data have;

input Sex;

datalines;

1

1

2

2

1

2

;

run;

data want;

   set have;

   format Sex num2ch.

run;

proc print data = want;

run;

proc contents data = want;

run;

But the type of SEX is still Number in WANT data set.

Grand Advisor
Posts: 16,925

Re: replacing contents of a variable

Not really without temporary variables. 

You can use a format to change the display, depending on what you're doing, a Class statement in many procs will accomplish this automatically.

Ask a Question
Discussion stats
  • 3 replies
  • 266 views
  • 0 likes
  • 4 in conversation