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

How do I assign one data type, or format, to multiple variables in a DATA step?

 

In the following code, I want patientid to be a character variable w/ length 18, like name. I know I can add $18 after patientid, but how would I assign one format, or data type, to multiple variables.

 

I also want age to be a numeric variable w/ length 3. The current syntax produces a length of 8 for the variable age.

 

Thank you for the help.

 

data club1;
   INPUT patientid name $18. weight height age 3.;
   datalines;
023 Bob Shaw       123 72 58
049 Joey Smith     185 68 78
;
RUN;

Variables in Creation Order
# Variable Type Len
1 patientid Num 8
2 name Char 18
3 weight Num 8
4 height Num 8
5 age Num 8

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You didn't specify length so the result is correct. I think you're expecting the length/format/informat to align and they don't...and they don't have to either.

 

You method of assigning to multiple variables is correct - list all and then the format/length to be applied.

 

length weight  height age 3.;

The above assigns a length of 3 to the variables.

 

 

View solution in original post

7 REPLIES 7
Reeza
Super User

You didn't specify length so the result is correct. I think you're expecting the length/format/informat to align and they don't...and they don't have to either.

 

You method of assigning to multiple variables is correct - list all and then the format/length to be applied.

 

length weight  height age 3.;

The above assigns a length of 3 to the variables.

 

 

FreelanceReinh
Jade | Level 19

Hi @_maldini_,

 

You should insert a LENGTH statement between the DATA and INPUT statements in order to define variable types and lengths:

length patientid name $18
       age 3;

(No periods are required after the length specifications as these are no formats.)

 

Formats can be assigned with the FORMAT or ATTRIB statement in the data step (or with PROC DATASETS).

 

Syntax example:

format date1 date2 yymmdd8.;

 

_maldini_
Barite | Level 11

@FreelanceReinh Use a LENGTH statement to assign length. Got it. If you use a LENGTH statement, you are also assigning the variable type in that statement, correct? So there is no need to also assign it in the INPUT statement. If you don't use a LENGTH statement, you would do assign the variable type in the INPUT statement. Is that correct?

Reeza
Super User
Not necessarily, a format and or informat statement may also implicitly declare the type so you don't need to specify it in the input statement. I highly recommend specifying as much as you know, the less in your input statement the less lIkely there are to be errors.
_maldini_
Barite | Level 11

@Reeza If you include a LENGTH statement, and an INFORMAT and FORMAT statements, where do you assign these attributes? Is it the first time the varaible is presented to SAS? For example, if I do not include a certain variable  (e.g. Time) in the LENGTH statement, SAS skips over it and it is absent in a PROC PRINT.

 

For example: 

 

DATA test1;
  LENGTH name $10 id 3;
  INFORMAT time TIME10.;
  *FORMAT time TIME8.;
  INPUT time name id;

DATALINES;
11:00PM Jamie 211
9:00PM Bob 322
12:30 Ron 333
2:00AM Joe 111
;
RUN;

 

However, if I include it in the LENGTH statement, as a character variable, and then assign an informat in an INFORMAT statement, the log states: "NOTE 485-185: Informat $TIME was not found or could not be loaded."

 

DATA test1;
  LENGTH time $8 name $10 id 3;
  INFORMAT time TIME10.;
  *FORMAT time TIME8.;
  INPUT time name id;

DATALINES; 11:00PM Jamie 211 9:00PM Bob 322 12:30 Ron 333 2:00AM Joe 111 ; RUN;

What am I doing wrong here? Thanks!

FreelanceReinh
Jade | Level 19
@_maldini_ wrote:

(...) If you use a LENGTH statement, you are also assigning the variable type in that statement, correct?

Yes, this is correct.



So there is no need to also assign it in the INPUT statement.

Exactly. To quote the documentation of the INPUT statement: "If the variable is previously defined as character, $ is not required."



If you don't use a LENGTH statement, you would do assign the variable type in the INPUT statement. Is that correct?

Not necessarily. By specifying the $ sign or a character informat for a variable in the INPUT statement, it will become a character variable if it has not been declared previously (otherwise a numeric variable). However, there are also other possible ways to define the variable type before the INPUT statement, in particular a FORMAT statement, an INFORMAT statement or an ARRAY statement.

Astounding
PROC Star

A couple of notes on this ...

 

If you assign a length to a numeric variable, the general rule of thumb is that it should contain integers only.  A length of 3 does not mean that 3 digits fit, but 4 do not.  Numerics are stored in a different form entirely.

 

Also, if you use a LENGTH or ATTRIB statement, it must go before the INPUT statement for character variables, but can go anywhere for numeric variables.

 

Behind the scenes, there are some technical issues and reasons for this.  But these rules are probably the useful parts of that information at this point in time.

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
  • 7 replies
  • 13772 views
  • 5 likes
  • 4 in conversation