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….
I tried doing all of this is one temporary dataset but it shows errors, please help. Thanks!
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 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….
- for one column for HeatingAC – I need to put Yes-1 and No as 0
- For ExternalQuality and KitchenQuality- I need to use 5- for Excellent, 4-Good, 3-Average, 2-Fair, 1-Poor.
- 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.
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;
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;
Thank you, that worked
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?
Thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.