BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PierreYvesILY
Pyrite | Level 9

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

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.);

 

 

PierreYvesILY
Pyrite | Level 9
This doesn't work at all.
ballardw
Super User

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.

 

PierreYvesILY
Pyrite | Level 9

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: sas_14012021.PNG

 

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;

 

 

 

 

ballardw
Super User

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;
PierreYvesILY
Pyrite | Level 9
weekv is the good choice.
PierreYvesILY
Pyrite | Level 9
Hello,

is there an analog way to extract the year?
20W5001 => 2020
PierreYvesILY
Pyrite | Level 9

Hello,

 

is there an analog way to extract the year?

20W5001 => 2020

 

regards

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 1310 views
  • 4 likes
  • 4 in conversation