BookmarkSubscribeRSS Feed
deleted_user
Not applicable
when using the if then statements, can you assign a character statemetn to a numberic variable adn visa versa?
4 REPLIES 4
deleted_user
Not applicable
this will make it easier to check the data once it has been run and make it easier to read for example i was trying to do this if age >= 1 and age < 4 then agegroup = toddler, but it didn't work.
Cynthia_sas
Diamond | Level 26
Hi:
You have a good idea. The way I would describe what you want to do is: create a new character variable or new category variable based on the VALUE of a numeric variable. Technically, you are not assigning a numeric statement to a character variable or a character value to a numeric variable.

There are 2 ways to do what you want:
1) is to create your own format and then use either the PUT function or INPUT function to create either a character or numeric variable
2) use a series of IF statements to assign a value to either a character or numeric variable.

There are some rules about creating variables. For example:
[pre]
newnumvar = 10; <-- numeric variable being created
newcharvar = 'Wombat'; <--character variable being created

OR

** NUMERIC variable AGECAT being created;
if age gt 0 and age lt 4 then agecat = 1;
else agecat = 2;

OR

** CHARACTER variable AGECAT2 being created;
if age gt 0 and age lt 4 then agecat2 = 'toddler ';
else agecat2 = 'pre-school';

[/pre]

Note that character variables are -usually- assigned quoted strings or assigned the values of other character variables. So when you had something like:
agegroup = toddler

SAS thought that toddler was a variable and you wanted to assign the value of the toddler variable to the new variable agegroup.

You might want to look in the documentation for the section about variables and how to create them and how to use them.

cynthia
deleted_user
Not applicable
thankyou for the information. I will run it and see how it comes out.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1175 views
  • 0 likes
  • 2 in conversation