DATA Step, Macro, Functions and more

SAS date formats

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

SAS date formats

Hello,

 

I am importing data from many csv files where the monthly date is entered as 2013m5 for May 2013, 2013m6 for June 2013 and so on. The letter m is between the year and the month.

 

What is the SAS date format I can use to read  from the csv files the dates to be more like May2013, June2013 and so on?

 

Thanks

Jack


Accepted Solutions
Solution
‎07-06-2016 04:12 PM
Respected Advisor
Posts: 4,644

Re: SAS date formats

I would try:

 

data &NAICSbankrupt;
format TYPE $36. CODE $6. TITLE $46. theDate monyy7.;
infile "&outroot\&NAICSBankrupt..csv" dsd missover firstobs=13;
informat TYPE $36. DATE $7. CODE $6. TITLE $46. TITRE $46.;
input TYPE DATE CODE TITLE TITRE NL NS PEI NB QB ON MN SK AB BC NWT YK NU CA;
theDate = input(cats(translate(DATE, "-", "m"), "-1"), anydtdte.);
label Type = 'Type'
      theDate = 'Date'
      Code = 'NAICS'
      Title = 'Title'
      Titre = 'Titre'
	  NL = 'NL'
	  NS = 'NS'
	  PEI ='PEI'
	  NB = 'NB'
	  QB = 'QB'
	  ON = 'ON'
	  MN = 'MN'
	  SK = 'SK'
	  AB = 'AB'
	  BC = 'BC'
	  NWT ='NWT'
	  YK = 'YK'
	  NU = 'NU'
	  CA = 'CA';
drop Titre DATE;
run; 

(untested)

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,644

Re: SAS date formats

[ Edited ]

You could read the dates as character strings (length>=7) and translate to SAS dates with something like:

 

date = input(cats(translate(dateStr, "-", "m"), "-1"), anydtdte.);

format date monyy7,;

PG
Contributor
Posts: 52

Re: SAS date formats

Thanks.

 

I inserted the code you provided but I couldn't get it to produce the dates correctly.  Here is my code -- where would I insert your recommeded coding?

 

Thanks...

 

data &NAICSbankrupt;
format TYPE $36. DATE $7. CODE $6. TITLE $46. TITRE $46. NL NS PEI NB QB ON MN SK AB BC NWT YK NU CA;
    infile "&outroot\&NAICSBankrupt..csv" dsd missover firstobs=13 obs=2234;
    informat TYPE $36. DATE $7. CODE $6. TITLE $46. TITRE $46. NL NS PEI NB QB ON MN SK AB BC NWT YK NU CA;
input TYPE DATE CODE TITLE TITRE NL NS PEI NB QB ON MN SK AB BC NWT YK NU CA;
label Type = 'Type'
      Date = 'Date'
      Code = 'NAICS'
      Title = 'Title'
      Titre = 'Titre'
	  NL = 'NL'
	  NS = 'NS'
	  PEI ='PEI'
	  NB = 'NB'
	  QB = 'QB'
	  ON = 'ON'
	  MN = 'MN'
	  SK = 'SK'
	  AB = 'AB'
	  BC = 'BC'
	  NWT ='NWT'
	  YK = 'YK'
	  NU = 'NU'
	  CA = 'CA';
drop Titre; 
run;

 

Solution
‎07-06-2016 04:12 PM
Respected Advisor
Posts: 4,644

Re: SAS date formats

I would try:

 

data &NAICSbankrupt;
format TYPE $36. CODE $6. TITLE $46. theDate monyy7.;
infile "&outroot\&NAICSBankrupt..csv" dsd missover firstobs=13;
informat TYPE $36. DATE $7. CODE $6. TITLE $46. TITRE $46.;
input TYPE DATE CODE TITLE TITRE NL NS PEI NB QB ON MN SK AB BC NWT YK NU CA;
theDate = input(cats(translate(DATE, "-", "m"), "-1"), anydtdte.);
label Type = 'Type'
      theDate = 'Date'
      Code = 'NAICS'
      Title = 'Title'
      Titre = 'Titre'
	  NL = 'NL'
	  NS = 'NS'
	  PEI ='PEI'
	  NB = 'NB'
	  QB = 'QB'
	  ON = 'ON'
	  MN = 'MN'
	  SK = 'SK'
	  AB = 'AB'
	  BC = 'BC'
	  NWT ='NWT'
	  YK = 'YK'
	  NU = 'NU'
	  CA = 'CA';
drop Titre DATE;
run; 

(untested)

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 268 views
  • 0 likes
  • 2 in conversation