DATA Step, Macro, Functions and more

Convert from NLNUM12. to Character

Reply
Regular Contributor
Posts: 243

Convert from NLNUM12. to Character

Can someone please tell me how to convert a variable from NLNUM12. to Character.

 

 

Many Thanks.

Super User
Posts: 23,343

Re: Convert from NLNUM12. to Character

Use the PUT() function.

 

 

Regular Contributor
Posts: 243

Re: Convert from NLNUM12. to Character

[ Edited ]

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;

Super User
Posts: 23,343

Re: Convert from NLNUM12. to Character

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.

Super User
Super User
Posts: 7,942

Re: Convert from NLNUM12. to Character

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.

 

 

 

 

Regular Contributor
Posts: 243

Re: Convert from NLNUM12. to Character

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. 

Super User
Super User
Posts: 7,942

Re: Convert from NLNUM12. to Character


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.

Regular Contributor
Posts: 243

Re: Convert from NLNUM12. to Character

[ Edited ]

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

Super User
Super User
Posts: 7,942

Re: Convert from NLNUM12. to Character

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.

Regular Contributor
Posts: 243

Re: Convert from NLNUM12. to Character

Thanks Tom. I did figure that out. Yes, you are right ATTRIB and LENGTH really worked well at the end.
Valued Guide
Posts: 560

Re: Convert from NLNUM12. to Character

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;
Thanks,
Suryakiran
Regular Contributor
Posts: 243

Re: Convert from NLNUM12. to Character

Posted in reply to SuryaKiran
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.
Super User
Posts: 13,358

Re: Convert from NLNUM12. to Character


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;
Super User
Posts: 13,358

Re: Convert from NLNUM12. to Character

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;

Ask a Question
Discussion stats
  • 13 replies
  • 124 views
  • 7 likes
  • 5 in conversation