hello dear SAS experts,
I use the following Code to select datalines by week:
%let Auswertungstag=%sysfunc(today());
Data zeitraum (keep=date);
format currdate yymmdd10. date weekv7. ;
Auswertungstag = &Auswertungstag.;
%kw(&Auswertungstag.);
Jahr = &Jahr1.;
currdate = input(cats(put(Jahr,z4.),'W',put(kw_kompakt,z2.)),weekv7.);
do date = currdate - 35 to currdate - 1 by 7;
output;
end;
run;
I get the results like this:
20W5001
20W5101
20W5201
20W5301
21W0101
I Need to extract the week number (1st. line: 50 ; 2nd line: 51 etc) and store it as a numeric value.
How can I proceed? I tried with substr(date,4,2) but it doesn't work.
Regards
You have the WEEK functions AFTER the only output statement. So the values are not set when the data is written.
try
do date = currdate - 35 to currdate - 1 by 7; number = input(substr(string,4,2),32.); weeku = week(date,'u'); weekv = week(date,'v'); weekw = week(date,'w'); output; end;
What are you starting with?
Do you have the strings like '20W5001' ? If so then just pull out the '50' and use INPUT() to convert it to a number.
If you have something else then please provide a much clearer example of what is your input and what is your desired output. Your current code seems to have little relationship to your question.
string = '20W5001' ;
number = input(substr(string,4,2),32.);
For any given actual date there are 3 different standard week number definitions that the Week function may use to get a week number. If the Date of interest that you have is using a WEEKV format then likely you need the Week function with the 'V' option to get a numeric value.
data example; format date weekv7.; do date='01jan2021'd to '31dec2021'd; weeku = week(date,'u'); weekv = week(date,'v'); weekw = week(date,'w'); output; end; run;
Just to show the three possible "week" values from a given date.
I don't get the expected results, so I guess I'm not using the functions properly:
Data zeitraum (keep=date number weeku weekv weekw);
format currdate yymmdd10. date weekv7. ;
Auswertungstag = &Auswertungstag.;
%kw(&Auswertungstag.);
Jahr = &Jahr1.;
currdate = input(cats(put(Jahr,z4.),'W',put(kw_kompakt,z2.)),weekv7.);
do date = currdate - 35 to currdate - 1 by 7;
output;
end;
number = input(substr(string,4,2),32.);
weeku = week(date,'u');
weekv = week(date,'v');
weekw = week(date,'w');
run;
which gives the following result:
In Addition, the kw macro is the following:
%macro kw(datum);
/* datum1 ist immer ein Donnerstag, da nach DIN 1355 die erste KW die Woche ist, in
der mindestens 4 Tage des neuen Jahres sind, s. Link oben zu Wiki */
datum1=&datum-mod(&datum-mdy(1,1,1900),7)+3;
korrektur1=mod(&datum-mdy(1,1,1900),7)-10;
datum2=mdy(1,1,year(datum1))+korrektur1;
tage=&datum-datum2;
kwcalc=int(tage/7);
kw_kompakt=int((&datum-(mdy(1,1,year(&datum-mod(&datum-mdy(1,1,1900),7)+3))+(mod(&datum-mdy(1,1,1900),7)-10)))/7);
%mend kw;
You have the WEEK functions AFTER the only output statement. So the values are not set when the data is written.
try
do date = currdate - 35 to currdate - 1 by 7; number = input(substr(string,4,2),32.); weeku = week(date,'u'); weekv = week(date,'v'); weekw = week(date,'w'); output; end;
For such a question, consult the documentation of SAS Functions and CALL Routines by Category .
Scroll down to the Date and Time section, and you will quickly find the YEAR Function.
Hello,
is there an analog way to extract the year?
20W5001 => 2020
regards
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.