BookmarkSubscribeRSS Feed
fox96
Fluorite | Level 6

I have my Date variable which is a column containing dates from March 2006 to September 2020. The dates are presented as follows: MAR2006, AVR2006 ... until SEP2020 in SAS. I would like to create a month variable and a variable year. When I do month = month (Date) and year = year (Date) I just have empty columns.

10 REPLIES 10
fox96
Fluorite | Level 6

I have my Date variable which is a column containing dates from March 2006 to September 2020. The dates are presented as follows: MAR2006, AVR2006 ... until SEP2020 in SAS. I would like to create a month variable and a variable year. When I do month = month (Date) and year = year (Date) I just have empty columns.

ChrisNZ
Tourmaline | Level 20

What's the type of your DATE variable? What does the log show?

fox96
Fluorite | Level 6
my variable is alphanumeric.I'm a new sas user,where can i see the log?
Kurt_Bremser
Super User

Depending on your interface, your code will have a Log window or Log tab.

Possible interfaces:

  • SAS Studio (web-based, used with SAS On Demand)
  • Enterprise Guide ("fat" client, Windows only)
  • Display Manager ("native" SAS interface, rarely used today)
ChrisNZ
Tourmaline | Level 20

You need to familiarise yourself with the log. Always check it, even when there seems to be no errors. Always.

You can change your string to a number by using the correct informat, monyy. in your case:

NUM=input(CHAR,monyy.);

To display your variable as a date use a date format, for example:

format NUM date9;

 

 

fox96
Fluorite | Level 6
I have tried that method, and it also gave me an empty column.I loaded my dataset on the forum so you can see.
Kurt_Bremser
Super User

In SAS, dates are counts of days, with 1960-01-01 as day zero.

Therefore, variables that contain dates (or date-related values like months, as in your case, or quarters) have to be numeric and have a date format assigned. I suspect that your variables are character, which will be documented in your log, so please post that.

Copy/paste the log text into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

AMSAS
SAS Super FREQ

Like the others said we are going to need to see your SAS log and probably an example of your data/code.

 

You probably need to review Working with Dates in the SAS System 

 

Here's an example that might help you understand, note that SAS stores date values as the number of days since 01JAN1960 (day 0). In the code below you will see myDate contains the number 22589 (05Nov2021) , the number of days since 01Jan1960.

 

data want ;
	/* Get todays date and store in variable myDate */
	myDate=today() ;
	/* Get the year part of myDate */
	year=year(myDate) ;
	/* Get the month part of myDate */
	month=month(myDate) ;
	/* send myDate (raw value), myDate formatted, year and month to log */ 
	put myDate= myDate= date7. year= month= ;
run ;

Here's what the log looks like when you run the above code, I highlighted the output from the put statement:

2451  data want ;
2452      myDate=today() ;
2453      year=year(myDate) ;
2454      month=month(myDate) ;
2455      put myDate= myDate= date7. year= month= ;
2456  run ;

myDate=22589 myDate=05NOV21 year=2021 month=11
NOTE: The data set WORK.WANT has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
fox96
Fluorite | Level 6

my code:

DATA ESTIMATIONS_ALL;
SET ESTIMATIONS_ALL;
month=month(Date);
year=year(Date);
RUN;

my log:

   ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='MATH30602.A2021_Devoir1_VotreMatricule.sas';
4          %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='C:\Users\axel_\OneDrive\Bureau\Logiciels statistiques\MATH30602.A2021_Devoir1_VotreMatricule.sas';
9          %LET _SASPROGRAMFILEHOST='LAPTOP-NMODV674';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=SVG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         %macro HTML5AccessibleGraphSupported;
15             %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16         %mend;
17         FILENAME EGHTML TEMP;
18         ODS HTML5(ID=EGHTML) FILE=EGHTML
19             OPTIONS(BITMAP_MODE='INLINE')
20             %HTML5AccessibleGraphSupported
21             ENCODING='utf-8'
22             STYLE=HtmlBlue
23             NOGTITLE
24             NOGFOOTNOTE
25             GPATH=&sasworklocation
26         ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27         
28         DATA ESTIMATIONS_ALL;
29         SET ESTIMATIONS_ALL;
30         month=month(Date);
31         year=year(Date);
32         RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      30:13   31:11   
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=1 Both sexes=10147.7 Male=4975.8 Female=5171.9 month=.
year=. _ERROR_=1 _N_=1
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=2 Both sexes=8609 Male=4296.6 Female=4312.4 month=. year=.
_ERROR_=1 _N_=2
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=3 Both sexes=857.7 Male=439.5 Female=418.1 month=. year=.
_ERROR_=1 _N_=3
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=4 Both sexes=1715.8 Male=875.7 Female=840.1 month=. year=.
_ERROR_=1 _N_=4
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=5 Both sexes=858.1 Male=436.2 Female=421.9 month=. year=.
_ERROR_=1 _N_=5
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
2                                                          Le Système SAS                             09:05 Friday, November 5, 2021

Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=6 Both sexes=3682.3 Male=1835.8 Female=1846.5 month=. year=.
_ERROR_=1 _N_=6
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=7 Both sexes=5551.5 Male=2763.4 Female=2788.1 month=. year=.
_ERROR_=1 _N_=7
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=8 Both sexes=8431.9 Male=4100.1 Female=4331.8 month=. year=.
_ERROR_=1 _N_=8
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=9 Both sexes=1869.2 Male=927.6 Female=941.6 month=. year=.
_ERROR_=1 _N_=9
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=10 Both sexes=1341.7 Male=657.5 Female=684.2 month=. year=.
_ERROR_=1 _N_=10
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=11 Both sexes=2880.4 Male=1336.7 Female=1543.7 month=.
year=. _ERROR_=1 _N_=11
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=1 groupe_age_id=12 Both sexes=1538.7 Male=679.2 Female=859.5 month=. year=.
_ERROR_=1 _N_=12
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=1 Both sexes=731 Male=323.4 Female=407.6 month=. year=.
_ERROR_=1 _N_=13
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=2 Both sexes=320.4 Male=150.6 Female=169.9 month=. year=.
_ERROR_=1 _N_=14
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=3 Both sexes=40.7 Male=19.9 Female=20.8 month=. year=.
_ERROR_=1 _N_=15
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=4 Both sexes=53.3 Male=25.5 Female=27.8 month=. year=.
_ERROR_=1 _N_=16
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=5 Both sexes=12.6 Male=5.6 Female=7 month=. year=. _ERROR_=1
_N_=17
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=6 Both sexes=64.9 Male=35.3 Female=29.6 month=. year=.
_ERROR_=1 _N_=18
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=7 Both sexes=141.6 Male=69.1 Female=72.5 month=. year=.
_ERROR_=1 _N_=19
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 30 colonne 13.
NOTE: Invalid numeric data, Date='Mar2006' , at ligne 31 colonne 11.
WARNING: Limit set by ERRORS= option reached.  Further errors of this type will not be printed.
Date=Mar2006 Geo_id=1 caracteristique_id=1 education_id=2 groupe_age_id=8 Both sexes=677.7 Male=297.9 Female=379.8 month=. year=.
3                                                          Le Système SAS                             09:05 Friday, November 5, 2021

_ERROR_=1 _N_=20
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      247800 à 30:7   247800 à 31:6   
NOTE: There were 247800 observations read from the data set WORK.ESTIMATIONS_ALL.
NOTE: The data set WORK.ESTIMATIONS_ALL has 247800 observations and 10 variables.
NOTE: DATA statement a utilisé (Durée totale du traitement) :
      real time           0.16 seconds
      cpu time            0.15 seconds
      

33         
34         %LET _CLIENTTASKLABEL=;
35         %LET _CLIENTPROCESSFLOWNAME=;
36         %LET _CLIENTPROJECTPATH=;
37         %LET _CLIENTPROJECTPATHHOST=;
38         %LET _CLIENTPROJECTNAME=;
39         %LET _SASPROGRAMFILE=;
40         %LET _SASPROGRAMFILEHOST=;
41         
42         ;*';*";*/;quit;run;
43         ODS _ALL_ CLOSE;
44         
45         
46         QUIT; RUN;
47         

beforebeforeafterafter

Kurt_Bremser
Super User

What I suspected. You don't have dates, you have strings that look like dates. Do this:

month = month(input(Date,monyy7.));
year = year(input(Date,monyy7.));

Or, even better, read the data as dates when it arrives in SAS; how you do this depends on the type of data source.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1569 views
  • 4 likes
  • 4 in conversation