BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RaginiS
Fluorite | Level 6

Hi, I am super new to SAS programming and need help,

I have a large SAS dataset of property prices information and I need to convert about 3 columns from character to numeric by assigning values 1,2.3….

  1. for one column for HeatingAC – I need to put Yes-1 and No as 0
  2. For ExternalQuality and KitchenQuality- I need to use 5- for Excellent, 4-Good, 3-Average, 2-Fair, 1-Poor.
  3. Dwellingtype- 1-Singlefamily, 2-Twofamily, 3-Duplex and so on;

I tried doing all of this is one temporary dataset but it shows errors, please help. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Either combine your steps or make sure they don't all start from the original file (realestate). If they do, they're unique files and the changes made don't carry over. I think this is what you were trying to  do, changes are in red.

 

data test1;
set home.realestate (rename=(HeatingAC=oldvar));
if oldvar = 'N' then HeatingAC = 0;
else HeatingAC= 1;
drop oldvar;
run;


data test2;
set test1 ;
array clean{*} $ _character_;  *are you sure you want all character variables here;

do i=1 to dim(clean);

if clean{i}='SingleFamily' then clean{i}= 1;
else if clean{i}='TwoFamily' then clean{i}=2;
else if clean{i}='Duplex' then clean{i}=3;
else if clean{i}='Townhome' then clean{i}=4;
else if clean{i}='Condo' then clean{i}=5;

end;

drop i;
RUN;

proc print data=test2;
run;

Let me know if that gets you closer. I'm not sure that using _character_ here makes sense, it will use all character variables including an ID or anything else. 

 


@RaginiS wrote:

Hi, This is what I have so far and it does not work-

 

libname Home '/folders/myfolders/home';


data test1;
set home.realestate (rename=(HeatingAC=oldvar));
if oldvar = 'N' then HeatingAC = 0;
else HeatingAC= 1;
drop oldvar;

 


data test1;
set home.realestate ;
array clean{*} $ _character_;

do i=1 to dim(clean);

if clean{i}='SingleFamily' then
clean{i}= 1;

if clean{i}='TwoFamily' then
clean{i}=2;

if clean{i}='Duplex' then
clean{i}=3;

if clean{i}='Townhome' then
clean{i}=4;

if clean{i}='Condo' then
clean{i}=5;
end;

drop i;
RUN;

proc print data=test1;
run;


 

View solution in original post

7 REPLIES 7
Reeza
Super User

@RaginiS wrote:

Hi, I am super new to SAS programming and need help,

I have a large SAS dataset of property prices information and I need to convert about 3 columns from character to numeric by assigning values 1,2.3….

  1. for one column for HeatingAC – I need to put Yes-1 and No as 0
  2. For ExternalQuality and KitchenQuality- I need to use 5- for Excellent, 4-Good, 3-Average, 2-Fair, 1-Poor.
  3. Dwellingtype- 1-Singlefamily, 2-Twofamily, 3-Duplex and so on;

I tried doing all of this is one temporary dataset but it shows errors, please help. Thanks!


Please show what you tried, your code and the log. You likely have a small issue that's easy to correct. 

 

Video tutorial is here. Ensure you create NEW variables to hold the new values as well, you cannot change the type of a variable on in the same data step. 

https://video.sas.com/detail/videos/sas-analytics-u/video/4573023399001/creating-a-new-column-in-sas...

RaginiS
Fluorite | Level 6

Hi, This is what I have so far and it does not work-

 

libname Home '/folders/myfolders/home';


data test1;
set home.realestate (rename=(HeatingAC=oldvar));
if oldvar = 'N' then HeatingAC = 0;
else HeatingAC= 1;
drop oldvar;

 


data test1;
set home.realestate ;
array clean{*} $ _character_;

do i=1 to dim(clean);

if clean{i}='SingleFamily' then
clean{i}= 1;

if clean{i}='TwoFamily' then
clean{i}=2;

if clean{i}='Duplex' then
clean{i}=3;

if clean{i}='Townhome' then
clean{i}=4;

if clean{i}='Condo' then
clean{i}=5;
end;

drop i;
RUN;

proc print data=test1;
run;

Reeza
Super User

Either combine your steps or make sure they don't all start from the original file (realestate). If they do, they're unique files and the changes made don't carry over. I think this is what you were trying to  do, changes are in red.

 

data test1;
set home.realestate (rename=(HeatingAC=oldvar));
if oldvar = 'N' then HeatingAC = 0;
else HeatingAC= 1;
drop oldvar;
run;


data test2;
set test1 ;
array clean{*} $ _character_;  *are you sure you want all character variables here;

do i=1 to dim(clean);

if clean{i}='SingleFamily' then clean{i}= 1;
else if clean{i}='TwoFamily' then clean{i}=2;
else if clean{i}='Duplex' then clean{i}=3;
else if clean{i}='Townhome' then clean{i}=4;
else if clean{i}='Condo' then clean{i}=5;

end;

drop i;
RUN;

proc print data=test2;
run;

Let me know if that gets you closer. I'm not sure that using _character_ here makes sense, it will use all character variables including an ID or anything else. 

 


@RaginiS wrote:

Hi, This is what I have so far and it does not work-

 

libname Home '/folders/myfolders/home';


data test1;
set home.realestate (rename=(HeatingAC=oldvar));
if oldvar = 'N' then HeatingAC = 0;
else HeatingAC= 1;
drop oldvar;

 


data test1;
set home.realestate ;
array clean{*} $ _character_;

do i=1 to dim(clean);

if clean{i}='SingleFamily' then
clean{i}= 1;

if clean{i}='TwoFamily' then
clean{i}=2;

if clean{i}='Duplex' then
clean{i}=3;

if clean{i}='Townhome' then
clean{i}=4;

if clean{i}='Condo' then
clean{i}=5;
end;

drop i;
RUN;

proc print data=test1;
run;


 

RaginiS
Fluorite | Level 6

Quick question, I was able to covert the character variables to numeric, I can see it when I run proc print but when I run proc contents it still shows as Char, what did I not do?

 

Reeza
Super User
You cannot change variable types on the fly. You need to assign it to new variables, so just duplicate your array with a set of new names and those can be numeric.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1893 views
  • 2 likes
  • 2 in conversation