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

Hi everyone

 

I have a dataset containing data on a weekly basis. Each week has its own (character) variable - e.g. week 32 of 2010 is labelled y1032. What I want to do is to keep only data from 2018, 2019, and 2020 (keeping all weeks within a given year) - i.e keppeing 52*3 = 156 weeks:

 

- 1801, 1802, ... 1852.

- 1901, 1902, ... 1952

- 2001, 2002, ... 2052

 

I think an array should do the job. However, I'm having a hard time setting up a proper array as there is a "0" before the first 9 weeks within every year. Any suggestions on how to go over this?

Best,

Frederik

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Taasby,


@Taasby wrote:

Each week has its own (character) variable - e.g. week 32 of 2010 is labelled y1032. What I want to do is to keep only data from 2018, 2019, and 2020 (keeping all weeks within a given year) - i.e keppeing 52*3 = 156 weeks


Try this:

data want;
set have(keep=y18: y19: y20:);
run;

Or this:

data want;
set have(keep=y1801-y1852 y1901-y1952 y2001-y2052);
run;

In the second case, make sure that you don't miss a week 53. Add more variables to the KEEP= lists if needed.

View solution in original post

5 REPLIES 5
Oligolas
Barite | Level 11

Hi,

what does your data structure look like?

 

is this what you're looking for?

data have;
   input week $;
   datalines;
1801
1802
1852
1901
1902
1952
2001
2002
2052
1701
1702
1752
2101
2102
2152
;
run;

data want;
   set have;
   if '18'<=:week<=:'20';
run;

 

________________________

- Cheers -

Taasby
Calcite | Level 5
datalines;
1801 1
1802 1
1803 0
1804 0
1805 1
.
.
.
1851 0
1901 1
1902 0

Each variable is a dummy variable taking on the value of 0 or 1. It is looking like the above, but it would be orded horizontally in the SAS dataset as every week is a variable on its own. 

Best,

Frederik

Kurt_Bremser
Super User

@Taasby wrote:

.... as every week is a variable on its own. 


which is always a VERY BAD IDEA. Do not keep data in structure, see Maxim 19.

As you can see, with a long dataset layout the code practically writes itself.

If you receive data in a wide layout, always transpose to long immediately upon import.

FreelanceReinh
Jade | Level 19

Hi @Taasby,


@Taasby wrote:

Each week has its own (character) variable - e.g. week 32 of 2010 is labelled y1032. What I want to do is to keep only data from 2018, 2019, and 2020 (keeping all weeks within a given year) - i.e keppeing 52*3 = 156 weeks


Try this:

data want;
set have(keep=y18: y19: y20:);
run;

Or this:

data want;
set have(keep=y1801-y1852 y1901-y1952 y2001-y2052);
run;

In the second case, make sure that you don't miss a week 53. Add more variables to the KEEP= lists if needed.

Taasby
Calcite | Level 5

It did the job! Cheers! 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 521 views
  • 0 likes
  • 4 in conversation