BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sukumark
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
RichardinOz
Quartz | Level 8

Use the PUTN() function

Data want ;

     set have ;

     result_C = PUTN (result_N, fmtname) ;

Run ;

(Untested)

Richard

View solution in original post

5 REPLIES 5
RichardinOz
Quartz | Level 8

Use the PUTN() function

Data want ;

     set have ;

     result_C = PUTN (result_N, fmtname) ;

Run ;

(Untested)

Richard

RichardinOz
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sukumark
Calcite | Level 5

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

RichardinOz
Quartz | Level 8

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

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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