Can someone please tell me how to convert a variable from NLNUM12. to Character.
Many Thanks.
Use the PUT() function.
Hi Reeza,
I still can't get rid of the comma in my VARnew variable. Can you please help. Also please remember on my original data The NLNUM variable's FORMAT is 12. and INFORMAT32. Thanks.
DATA Have;
INPUT ID VAR1$ VAR4 VAR6$;
FORMAT VAR4 NLNUM12.;
DATALINES;
101 ENG1 15225555 NY
105 Che1 10222541 NY
109 Eng2 1511445 CA
115 Phy2 1156858 PA
201 Che1 14445555 TX
;
RUN;
DATA Want;
VARnew=put(VAR4, NLNUM12.);
SET Have;
RUN;
The format included in the PUT statement should be the one you want the data to look like, not what it currently looks like. Try Best32 as a starter if you’re not sure.
If you don't want the comma then why did you request it?
char_var = put(num_var,12.);
Also don't try to use a variable before it has been populated with something!!
Because you put the assignment statement before the SET statement your sample code is going to set the new variable to match the value from the previous observation.
Sorry folks, I still can't see the data under converted variable. In my original data my old variable features are:
TYPE Numeric
LENGTH 8
FORMAT BEST12.
INFORMAT BEAT32.
Example of my data: 61,983,986
When I convert it to char_var = put(num_var, 8.) I get only 6 instead of 61,983,986 and when I convert with 12. I get nothing on my new character variable.
@mlogan wrote:
Sorry folks, I still can't see the data under converted variable. In my original data my old variable features are:
TYPE Numeric
LENGTH 8
FORMAT BEST12.
INFORMAT BEAT32.
Example of my data: 61,983,986
When I convert it to char_var = put(num_var, 8.) I get only 6 instead of 61,983,986 and when I convert with 12. I get nothing on my new character variable.
We would need to see the full code, a value of 61 million should be able to be represented by 8 digits. Now if you have values of 100 million or more then 8 digits is not going to be long enough.
Perhaps you accidentally defined the new variable as length $1 and so there was only room to store the first digit.
Thanks for your reply Tom. Maximum length of the data is 61,983,986
Would you please tell me what would be the length size to show this value up as character. I am very sure that I defined the value as 8 but for some reason it shows one digit only. If I define the length as 7 or below, it still shows 1 digit.
Thanks
You need to show what you did.
It is best to use a LENGTH or ATTRIB statement to define the variable instead of assuming that the side effect of using the variable in some other statement will cause it to be defined properly.
Make sure to define your variables before you use them. That is before setting any dataset with that has the variable in it. Before any statement that references the variable, like an assignment statement, an IF statement, a FORMAT or INFORMAT statement, a PUT statement, etc.
So if I have an existing dataset named HAVE that has a numeric variable name NUM_VAR (it doesn't matter what display format is attached to it) and I want to make a character variable that has the digits that it would take to represent that number as an integer I could do.
data want ;
set have ;
length char_var $8 ;
char_var = put(num_var, 8.);
run;
That will store the number RIGHT ALIGNED in the 8 characters of the new variable. So a value like 1,234,567 would have a space as the first character.
' 1234567'
Also if your variable does exist already in an input dataset then make sure that you are not getting the wrong format attached to it. If you set XXX='61983986' and the display it with the $1. format then only the '6' will print.
Use compress to ignore commas. Also your new variable has to be created after the SET statement.
DATA Want;
SET Have;
VARnew=compress(put(VAR4, NLNUM12.),',');
RUN;
@mlogan wrote:
I still can't see the data under converted variable. In my original data my old variable features are:
TYPE Numeric
LENGTH 8
FORMAT BEST12.
INFORMAT BEAT32.
Example of my data: 61,983,986
When I convert it to char_var = put(num_var, 8.) I get only 6 instead of 61,983,986 and when I convert with 12. I get nothing on my new character variable.
Show the exact code you are using.
And since you seem to be having some problem, post the code from the log and any messages you get when running the code.
If your variable char_var already exists in your data set on the set statement then it will inherit the characteristics of that version of the variable. Which could be only 1 character if left over from some other code attempt previously.
OR if the first "value" is only one digit if used in an assignment like
if var1 = 'MAT1' THEN VARNEW='9';
The length for varnew has been set to 9 implicitly. If that is the case then explicitly specify a length for the character variable long
DATA Have; INPUT ID VAR1$ VAR4 VAR6$; FORMAT VAR4 NLNUM12.; DATALINES; 101 MAT1 61983986 NY 101 ENG1 61983986 NY ; RUN; DATA Wantwrong; SET Have; if var1 = 'MAT1' THEN VARNEW='9'; else VARnew=put(VAR4, best12. -L); RUN; DATA Wantright; SET Have; length varnew $ 12; if var1 = 'MAT1' THEN VARNEW='9'; else VARnew=put(VAR4, best12. -L); RUN;
Note that because of the location of your PUT statement the NewVar is for the version of Var4 on the previous line.
Also you may get leading blanks depending on the original value.
Try this if you do not want leading blanks for VARnew.
DATA Want; SET Have; VARnew=put(VAR4, best12. -L); RUN;
drop the -L if you want leading blanks.
Or use your existing data and change the format to BEST12 as needed.
Proc print data=have;
format var4 best12.;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.