DATA Step, Macro, Functions and more

Assign one data type, or format, to multiple variables in a DATA step

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

Assign one data type, or format, to multiple variables in a DATA step

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

 


Accepted Solutions
Solution
‎03-16-2016 08:37 PM
Super User
Posts: 19,869

Re: Assign one data type, or format, to multiple variables in a DATA step

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


All Replies
Solution
‎03-16-2016 08:37 PM
Super User
Posts: 19,869

Re: Assign one data type, or format, to multiple variables in a DATA step

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.

 

 

Trusted Advisor
Posts: 1,118

Re: Assign one data type, or format, to multiple variables in a DATA step

Hi @jcorroon,

 

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.;

 

Regular Contributor
Posts: 199

Re: Assign one data type, or format, to multiple variables in a DATA step

Posted in reply to FreelanceReinhard

@FreelanceReinhard 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?

Super User
Posts: 19,869

Re: Assign one data type, or format, to multiple variables in a DATA step

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.
Regular Contributor
Posts: 199

Re: Assign one data type, or format, to multiple variables in a DATA step

[ Edited ]

@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!

Trusted Advisor
Posts: 1,118

Re: Assign one data type, or format, to multiple variables in a DATA step

jcorroon 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.

Super User
Posts: 5,516

Re: Assign one data type, or format, to multiple variables in a DATA step

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 835 views
  • 5 likes
  • 4 in conversation