BookmarkSubscribeRSS Feed
User_Help
Obsidian | Level 7

Hi all,

 

I have a listing comprises of Numeric and Character, currently SAS recognized listing as character.

How to produce as numeric.

 

Var

1

2

2A

2B

3

 

IF VTYPE(VAR)=’N’ THEN VAR1 = input(VAR,2.);
ELSE VAR1 = PUT(VAR,2.);  

 

What's wrong with the code?

8 REPLIES 8
mkeintz
PROC Star

A variable can only be one type: numeric or character. This holds through every observation in the data set - otherwise how could you reliably code something like sumvar=var1+var2;

 

It is most definitely NOT the same as a spreadsheet, where each cell can be of a different type than other cells in the same column (which is the analog of a variable).  That is an attribute of spreadsheets that has to be abandoned in SAS (or STATA or SPSS or other statistical packages).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
User_Help
Obsidian | Level 7

If i would like to treat numeric as numeric, whereas character remains as character, can it be done?

ballardw
Super User

@User_Help wrote:

If i would like to treat numeric as numeric, whereas character remains as character, can it be done?


With two variables. Create a numeric variable for the values you want to treat as numeric.

If you do not need to do calculations, add, subtract, multiply and so on, with a variable then it should not be numeric at all.

User_Help
Obsidian | Level 7
could share how is the formula likes? Thanks.
Ksharp
Super User

You want this ?

 

data have;
input Var $;
cards;
1
2
2A
2B
3
;

data want;
 set have;
 var_num=input(var,?? best.);
 if missing(var_num) then var_char=var;
run;
User_Help
Obsidian | Level 7

Hi, 

 

"Var" the list is in Text format.

Can i get the "Expected Result" as screenshot below?

1,2,3 is in General format, 2A, 2B is Character format. Result shown in single column.

 

allenpee_0-1609893782573.png

 

SASKiwi
PROC Star

@User_Help  - Text Format and General Format are Excel concepts and have no meaning in SAS. From what I can see Column A and Column D are identical and all you are doing is changing the Excel format. You don't need SAS to do that. It's a lot easier just using Excel to format columns A and D.

Ksharp
Super User
NO. You can't .As SASKiwi mentioned , in SAS a column could be either character or numeric ,you can't have both in a column .

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 2318 views
  • 1 like
  • 5 in conversation