Help using Base SAS procedures

input with mix of data (numerical, character, date)

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

input with mix of data (numerical, character, date)

Hi to everyone, in order to understand the way to mix different kind of charater data, numerical data, and informat data as date,

this input is reading correctly,

but I would like someone can explain me the role of the colon and ampersand in this code,

necessary to make it works.

Thanks,

V.

data new3;

format dat date9. date mmddyy10.;

input obs subjid pp ae: & $14. dat: date9. trt: $10. date: mmddyy10. treatment $10.;

datalines;

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

  1       1000000234     0  lung left pain       23jan34       placebo   10/10/1995  drug

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

;

run;


Accepted Solutions
Solution
‎06-02-2012 10:36 AM
Super User
Super User
Posts: 7,078

Re: input with mix of data (numerical, character, date)

The & lets you read character strings with embedded single blanks.  In your example the AE variable.

The : tells SAS not to take the informat literally.  Instead of reading 14 characters for the treatment code 'placebo' it will stop when it sees the space. This will prevent it from "eating" (a very techinical term) the beginning of the date variable.

I usually find that it is better to use a LENGTH statement or ATTRIB statements to define the variables BEFORE the input statement.  This also has the advantage of allowing you to set the order that the variables will appear in your dataset.  Currently DAT and DATE will appear first because they are mentioned first (in the FORMAT statement).

You can also assign INFORMATs for the variables that need them (such as date and time values).

Then the input statement is much easier.  For your data the only thing that you would need special in your INPUT statement would be the & because of the embedded delimiters in the value of AE variable.  You might want to used a delimiter other than space.

data new4;

length obs subjid pp 8 ae $14 dat 8 trt $10 date 8 treatment $10;

informat dat date9. date mmddyy10.;

format dat date yymmdd10.;

input obs subjid pp ae & dat trt date treatment;

datalines;

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

  1       1000000234     0  lung left pain       23jan34       placebo   10/10/1995  drug

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

;

View solution in original post


All Replies
Solution
‎06-02-2012 10:36 AM
Super User
Super User
Posts: 7,078

Re: input with mix of data (numerical, character, date)

The & lets you read character strings with embedded single blanks.  In your example the AE variable.

The : tells SAS not to take the informat literally.  Instead of reading 14 characters for the treatment code 'placebo' it will stop when it sees the space. This will prevent it from "eating" (a very techinical term) the beginning of the date variable.

I usually find that it is better to use a LENGTH statement or ATTRIB statements to define the variables BEFORE the input statement.  This also has the advantage of allowing you to set the order that the variables will appear in your dataset.  Currently DAT and DATE will appear first because they are mentioned first (in the FORMAT statement).

You can also assign INFORMATs for the variables that need them (such as date and time values).

Then the input statement is much easier.  For your data the only thing that you would need special in your INPUT statement would be the & because of the embedded delimiters in the value of AE variable.  You might want to used a delimiter other than space.

data new4;

length obs subjid pp 8 ae $14 dat 8 trt $10 date 8 treatment $10;

informat dat date9. date mmddyy10.;

format dat date yymmdd10.;

input obs subjid pp ae & dat trt date treatment;

datalines;

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

  1       1000000234     0  lung left pain       23jan34       placebo   10/10/1995  drug

  1       1000000134     0  headache       23jan63       placebo   10/10/2005  drug

;

🔒 This topic is solved and locked.

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

Discussion stats
  • 1 reply
  • 169 views
  • 0 likes
  • 2 in conversation