DATA Step, Macro, Functions and more

Length and declaring a variable

Reply
Occasional Contributor
Posts: 5

Length and declaring a variable

I have a dataset which contains Date as  20-11-2017 format, time  as 00:12:58 and status variable of length 35.

I want to retain the same data format as above and for Time i need 12 hr format , for status variable i took statu $ but it gives only first 8 character since the default is 8 however i need full length.

 

Kindly Advice.

Super User
Super User
Posts: 7,979

Re: Length and declaring a variable

Post test data in the form of a datastep, follow this post if you need to:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

Then show what you want the output to look like.  Your post doesn't make sense as it is.

Occasional Contributor
Posts: 5

Re: Length and declaring a variable

Request id,Date,Request time,Dropoff time,Pickup point,Driver id,Status
1,11-07-2016,00:11:31,12:43:12 AM,Airport,285,Trip Completed
2,11-07-2016,00:12:58,NA,Airport,NA,No Cars Available

 

 

My Data looks as above.

PROC Star
Posts: 7,487

Re: Length and declaring a variable

Does the following do what you want?:

 

data have;
  informat date date9.;
  informat time time8.;
  informat status $35.;
  input date time status &;
  cards;
20nov2017 00:12:58 this is the time for all good men
;

data want;
  format date ddmmyyd10.;
  format time timeampm12.;
  format status $35.;
  set have;
run;

Art, CEO, AnalystFinder.com

 

Trusted Advisor
Posts: 1,583

Re: Length and declaring a variable

What kind of input dataset you have ? Is it a sas dataset or a flat file like .csv or .txt ?

 

You said that:  status variable of length 35 ... but it gives only first 8 characters 

That makes me think it is a flat file. I guess next code may help you:

 

data have;
     infile datalines truncover ;
     input date ddmmyy10. @+1 time time8. @+1 status $35. ;
    format date ddmmyy10. time time8.;
datalines;
20-11-2017 00:12:58 status is given as free text
25-12-2017 01:15:00 status is a dummy line
;
run;

The result, 1st line will be like:

20/11/2017  0:12:58 status is given as free text

 

the date with / instead - and time without leading zero

 

PROC Star
Posts: 7,487

Re: Length and declaring a variable

You could take care of all of your NA instances, and desired formatting, with something like:

 

proc format;
  invalue dmissing
  'NA'=.
  other=[anydtdtm.]
  ;
  invalue nmissing
  'NA'=.
  other=[8.]
  ;
run;

data want;
  informat date ddmmyy10.;
  informat Request_time Dropoff_time dmissing.;
  informat status $35.;
  informat Driver_ID nmissing.;
  format date ddmmyyd10.;
  format Request_time Dropoff_time timeampm12.;
  format status $35.;
  infile cards dlm=',';
  input Request_id Date Request_time Dropoff_time Pickup_point $ Driver_id Status &;
  cards;
1,11-07-2016,00:11:31,12:43:12 AM,Airport,285,Trip Completed
2,11-07-2016,00:12:58,NA,Airport,NA,No Cars Available
;

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 5

Re: Length and declaring a variable

It is CSV file having data as below

 

Request id,Date,Request time,Dropoff time,Pickup point,Driver id,Status
1,11-07-2016,00:11:31,12:43:12 AM,Airport,285,Trip Completed
2,11-07-2016,00:12:58,NA,Airport,NA,No Cars Available
3,11-07-2016,00:12:58,1:19:12 AM,Airport,80,Trip Completed

 

I want to read the whole data as is also i need only fields Request id,Date,Request time and Status , and i wrote the belo code.

 

data uber;
infile '/folders/myfolders/datafiles/uber.csv' delimiter= ',' firstobs=2;

input request_id Date DDMMYY10. status $35;
run;

 

 

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 151 views
  • 0 likes
  • 4 in conversation