Create a new character variable!

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Create a new character variable!

In below example, I would like to create a new character variable and with the attributes of second variable(FMTNAME)!! Resulting variable is RESULT_C.

RESULT_N       FMTNAME

5                       best.

5                         z5.

5                     dollar5.2

RESULT_C

5

00005

$5.00

Thanks in Advance,

Sukumar


Accepted Solutions
Solution
‎03-17-2014 02:14 AM
Super Contributor
Posts: 644

Re: Create a new character variable!

Use the PUTN() function

Data want ;

     set have ;

     result_C = PUTN (result_N, fmtname) ;

Run ;

(Untested)

Richard

View solution in original post


All Replies
Solution
‎03-17-2014 02:14 AM
Super Contributor
Posts: 644

Re: Create a new character variable!

Use the PUTN() function

Data want ;

     set have ;

     result_C = PUTN (result_N, fmtname) ;

Run ;

(Untested)

Richard

Super Contributor
Posts: 644

Re: Create a new character variable!

You will probably also need a Length statement:

Data want ;

     set have ;

     Length result_C $ 5 ;

     result_C = PUTN (result_N, fmtname) ;

     keep result_C ;

Run ;

(Untested)

Richard

Super User
Super User
Posts: 7,407

Re: Create a new character variable!

Just to note RichardinOz, best. will return 8 characters but default so will be truncated by the $5.  Pop a strip() around the putn, or increase the with to $10.

New Contributor
Posts: 2

Re: Create a new character variable!

Thank you for the answer! Is there any conventional method other than PUTN() ?

Super Contributor
Posts: 644

Re: Create a new character variable!

The PUT functions PUT(), PUTN(), and PUTC(), are the SAS conventional methods for recasting or recoding values.  PUTN() and PUTC() (for character values) allow the format to be provided dynamically, as you require in this case.

@RW9's amendment , or the use of the LEFT() function, is required if you use the modified version I supplied.

Richard

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 234 views
  • 1 like
  • 3 in conversation