DATA Step, Macro, Functions and more

how do you separate character variables, numeric variables and mixed these variables?

Reply
New Contributor
Posts: 4

how do you separate character variables, numeric variables and mixed these variables?

[ Edited ]

data temp1;

input a b c d$;

datalines;

1 1 1 x

1 1 1 y

1 1 1 z

;

run;

Super User
Super User
Posts: 7,401

Re: how do you separate character variables, numeric variables and mixed these variables?

I am sorry, your question is not clear.  Also note your test data is incorrect (typo in datalines, column d is never read in).  

There are defaults in SAS, as you have not supplied $ in the input line, then the data defaults to numeric, although the final column would not as it is character.

Valued Guide
Posts: 797

Re: how do you separate character variables, numeric variables and mixed these variables?

What do you mean by "separate".?  Your program, (after correction of spelling in datalines statement)  as it stands, reads three numeric vars, and ignores the 4th data item in each line of data.

 

If you want to read the 4th field, you need a 4th varname in the input statement, along with an input  format to tell sas it is a character variable.

 

 input a b c    d $1. ;

Super User
Posts: 10,500

Re: how do you separate character variables, numeric variables and mixed these variables?

Please be a bit more specific on your question. What do you mean my "separate"?

 

If you mean make a varaible numeric or character the way the variable is created will set whether it is numeric or character. By default most variables will be numeric. SAS does not have any "mixed" type. A variable is either numeric or character. If you want to see the character values the variable must be character.

 

In a simple example such as you provide using $ after the variable name will tell SAS to read the last column as character. The default when done this way will hold a maximum of 8 characters. If you specify an INFORMAT or LENGTH statement for the varaible before use you can set the length longer to hold a longer value.

 

data temp1;

input a b c letter $;

datallines;

1 1 1 x

1 1 1 y

1 1 1 z

;

run;

 

data temp1;

Length letter $ 25; /*<= set maximumn number of characters a variable will hold to 25*/

input a b c letter $;

datalines; /*<= corrected spelling*/

1 1 1 x

1 1 1 y

1 1 1 z

2 2 2 Amuchlongerstring

;

run;

 

or

data temp1;

informat letter $25. ; /*<=read variable using 25 characters*/

input a b c letter $;

datalines;

1 1 1 x

1 1 1 y

1 1 1 z

2 2 2 Amuchlongerstring

;

run;

Also you can enclose a value in single or double quotes in an assignment statement

data temp1;

informat letter $25. ; /*<=read variable using 25 characters*/

input a b c letter $;

anothervar = 'This is a different string value'; /*<= note that when using this approach without a Length statement the length will the that of the first value encountered in the code in this case 32*/

datalines;

1 1 1 x

1 1 1 y

1 1 1 z

2 2 2 Amuchlongerstring

;

Ask a Question
Discussion stats
  • 3 replies
  • 206 views
  • 0 likes
  • 4 in conversation