I'm looking to convert ZipCd from a numeric variable to a character variable. The code below gives me no errors or warnings, but it is not converting the variable. What am I doing wrong? Is it related to the number within the PUT statement? (To be honest, I'm not totally sure what it does). Thanks for any help.
%LET CourseRoot = /home/u63571562/BIOS6680_2023; LIBNAME HypImpt "&CourseRoot/Hypertension Study/Data/2_Import"; DATA HypImpt.IowaResidents; SET HypImpt.IowaResidents; ZipCd = PUT(NewZipCd, 2.); RUN; PROC PRINT DATA = HypImpt.IowaResidents; VAR ZipCd NewZipCd; RUN;
US zip codes are 5 digits long so your SAS format is too short - 2. should be 5.
Thank you for the response, but this did not change the outcome.
I can't tell from the information you've provided, which is your source variable and which is your target variable. You could have the variables swapped around in your PUT statement. The source variable must be inside the PUT function, and must be populated and numeric.
ZipCd is my source variable. I am looking for a statement that converts the numeric values to character values in the new variable (which I name NewZipCd).
We cannot tell why (or even whether) SAS is not making ZIPCD as character since you neither provided the PROC CONTENTS of the input dataset, HypImpt.IowaResidents, nor the SAS log.
If ZIPCD is not character then it is because ZIPCD already exists as a numeric variable in that dataset. You cannot change the type of a variable once it has been defined already.
What happens if you remove ZIPCD from the input dataset?
DATA HypImpt.IowaResidents;
SET HypImpt.IowaResidents (drop=zipcd) ;
ZipCd = PUT(NewZipCd, 2.);
RUN;
Attached are the PROC CONTENTS (Variable 5 is what I am looking to have converted to character). Dropping ZipCd did not change anything. Here is the log:
Errors Warnings Notes (5) 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 68 69 DATAHypImpt.IowaResidents; 70 SETHypImpt.IowaResidents (drop=ZipCd); 71 72 ZipCd= PUT(NewZipCd, 5.); 73 74 RUN; NOTE: There were 196 observations read from the data set HYPIMPT.IOWARESIDENTS. NOTE: The data set HYPIMPT.IOWARESIDENTS has 196 observations and 11 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds user cpu time 0.01 seconds system cpu time 0.00 seconds memory 965.81k OS Memory 22184.00k Timestamp 10/03/2023 03:46:43 AM Step Count 207 Switch Count 2 Page Faults 0 Page Reclaims 167 Page Swaps 0 Voluntary Context Switches 44 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 264 75 76 PROC PRINT DATA = HypImpt.IowaResidents; 77 VAR ZipCd NewZipCd; 78 RUN; NOTE: There were 196 observations read from the data set HYPIMPT.IOWARESIDENTS. NOTE: PROCEDURE PRINT used (Total process time): real time 0.08 seconds user cpu time 0.08 seconds system cpu time 0.00 seconds memory 1158.62k OS Memory 22180.00k Timestamp 10/03/2023 03:46:43 AM Step Count 208 Switch Count 0 Page Faults 0 Page Reclaims 74 Page Swaps 0 Voluntary Context Switches 10 Involuntary Context Switches 0 Block Input Operations 288 Block Output Operations 40 79 79 ! PROC CONTENTS; 80 81 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 91
Don't do this:
DATA HypImpt.IowaResidents;
SET HypImpt.IowaResidents;
If something untoward happens in the step, you destroy the dataset you start with.
Rerun the process you used for setting up your course data, then run
proc print data=hypImpt.iowaresidents (obs=10);
run;
Given the fact that most of your variables are named varX (X being a number), and both newzipcd and zipcd appear at the physical end, I suspect that neither variable was part of the dataset to begin with.
The answer is hidden within the replies you have received.
As you know, this statement fails to change the numeric ZipCd to the character NewZipCd.
ZipCd = PUT(NewZipCd, 2.);
The correct statement would be:
NewZipCd = PUT(ZipCd, Z5.);
The PUT function converts from numeric to character. (Just for the record, it can also convert from character to character.) The Z5 format requests 5 digits, with leading zeros as needed to flll out the five characters.
SAS statements that assign a value require the new variable name being created to appear before the equal sign, and the value to assign should appear to the right of the equal sign.
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.