BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Thomas_mp
Obsidian | Level 7

Hello,

I need to create a new variable that adds the character “_" in front of the value of an existing variable.  I find this easy to do in Excel, but I cannot  do it in SAS.

An example:

 

This is what I have

 

data have   ; input  date  va   vb  $;

cards  ;

20000630    6.1207   to

20000929    6.1158   ri

20001229    5.7898   to

20010330    4.9815   co

;

run;

 

I need to generate new variable _va that adds _ to the values of the existing variable  va.

So, I would  like to have this:

data want

Date                   va                      _va        vb

20000630    6.1207            _6.1207     to

20000929    6.1158            _3.1158     ri

20001229    5.7898            _5.7898     to

20010330    4.9815            _4.9815     co

 

Thank you for your help

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Doing what you ask should be as simple as:
_va = catt("_", va);
However, you have to realize that your new variable must be a character variable. Numeric variables cannot contain an underscore. Other approaches are worth considering, such as a picture format that contains a prefix of an underscore, but leaves the actual value as numeric. You would have to describe what happens next, to get a more insightful answer. What are your plans for using _va?.

Also note, it would be worth learning how SAS handles dates. Instead of an 8 digit number, there is a better way to go.

View solution in original post

5 REPLIES 5
Astounding
PROC Star
Doing what you ask should be as simple as:
_va = catt("_", va);
However, you have to realize that your new variable must be a character variable. Numeric variables cannot contain an underscore. Other approaches are worth considering, such as a picture format that contains a prefix of an underscore, but leaves the actual value as numeric. You would have to describe what happens next, to get a more insightful answer. What are your plans for using _va?.

Also note, it would be worth learning how SAS handles dates. Instead of an 8 digit number, there is a better way to go.
Tom
Super User Tom
Super User

Certainly much easier to do in SAS than in Excel.  No pointing or clicking required.

data want;
   set have;
   length _va $15 ;
   _va=cats('_',va);
run;

You can also use the PUT() function to control how the number is converted to a string. 

 

If you know VA should have positive values values less than 10 then you probably want to use the 6.4 format.

data want;
   set have;
   length _va $7;
   _va=cats('_',put(va,6.4));
run;
andreas_lds
Jade | Level 19

I am curious: what is the use-case requiring numeric values being prefixed with an underscore?

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 968 views
  • 3 likes
  • 5 in conversation