DATA Step, Macro, Functions and more

Converting Date columns into Char.

Reply
Super Contributor
Posts: 276

Converting Date columns into Char.

Hi...

I am having the  following code

data xx;

input folder $ date ddmmyy10.;

format date ddmmyy10.;

cards;

week1 21/05/2012

week2 21/05/2011

week3 21/05/2010

;

run;

data yy;

set xx;

format week1 week2 week3 date9.;

if folder = 'week1'  then week1 = date;

if folder = 'week2' then week2 = date;

if folder = 'week3' then week3 = date;

run;

I want to display  '  '(space)  in the place of ' . '(period) , whenever the date is missing the Week1,week2,week3 columns .

Note:I don't want to use any IF conditions.

Thanks in advance ..

Regards,

Sanjeev.K

Super Contributor
Posts: 282

Re: Converting Date columns into Char.

Hi Sanjeev,

You could try using the following options statement before your code:

options missing=' ';

I wasn't sure if you wanted to replace your if statements as you noted, but if that is the case then you could use the select statement.

Regards,

Amir.

Super Contributor
Posts: 276

Re: Converting Date columns into Char.

Hi Amir..

IF i use Missing='' option,other numeric variables(which are not shown in the code)  also effected.but i want to keep periods in numeric variables when there is no data.

Thanks..

Sanjeev.K

Super Contributor
Posts: 282

Re: Converting Date columns into Char.

Hi Sanjeev,

I'm not sure how some numeric data can have missing represented by a blank and others be represented by a period at the same time

Perhaps consider using some default value which is not going to appear in your input data.

I did find this forum question https://communities.sas.com/message/139151#139151 but was unable to get the solution to fit around your problem.

Regards,

Amir.

Message was edited by: Amir Malik - rephrase.

Super User
Posts: 5,260

Re: Converting Date columns into Char.

To avoid your if-statements, use PROC TRANSPOSE.

I think as Amir it's hard to have different missing chars in he same report.

perhaps (untested!) you can create a new format based on ddmmyy, where you explicitly set the missing to space...?

Data never sleeps
Super User
Posts: 5,260

Re: Converting Date columns into Char.

Another easier option is to convert your date columns to char prior to reporting, and then replace . with space.

Data never sleeps
Contributor
Posts: 70

Re: Converting Date columns into Char.

Could you please provide what option is there.

Thanks & Regards,

Yaswanth J.

Super Contributor
Posts: 276

Re: Converting Date columns into Char.

Hi LinusH..

Did't get you completely...

Can you please write sample code for my above example.??

That make sense..

Thanks..

Sanjeev.K

Super User
Posts: 5,260

Re: Converting Date columns into Char.

If you report directly from your import data, use char informat directly:

input $date char10.;

The there will never be a period (unless your source data contains it...)

PROC TRANSPOSE, see online doc, stuffed with examples...

Data never sleeps
Respected Advisor
Posts: 3,908

Re: Converting Date columns into Char.

If there is not a very good reason then I wouldn't store dates as strings but as SAS date values. If you're using strings then one of the issues you're facing in reports is the sort order of the dates in your reports.

I believe using a user defined format as posted earlier will cover the requirement without causing new issues.

Respected Advisor
Posts: 3,908

Re: Converting Date columns into Char.

You could create your own format based on an existing one like done in below code:

proc format;
  value My_ddmmyy
    .=' '
    other=[ddmmyy.]
  ;
run;

data have;
  input folder $ date ddmmyy10.;
  format date My_ddmmyy10.;
cards;
week1 21/05/2012
week2
week3 21/05/2010
;
run;

Super User
Posts: 10,552

Re: Converting Date columns into Char.

Patrick's response is a very good idea in general. When only one or two values need special appearance make a custom version of the format like this. It can save a great deal of complex coding.

Ask a Question
Discussion stats
  • 11 replies
  • 335 views
  • 3 likes
  • 6 in conversation