BookmarkSubscribeRSS Feed
andreas_schmitz
Calcite | Level 5

Hi Community,

I have a table like this:

AK_2014AK_2015AK_2016AK_2017AK_2018AK_2019
0100002000
500030010
25000170

I would like to create a data step and have to access the columns depending on the input date. For example:

Data test;

set AK_Table;

y=year(input_date);

If AK_y >0 then;

Result1=AK_y;

Else if AK_(y-1) >0 then;

Result1=AK_(y-1);

.....

run;

But obviously this is not the correct way, since SAS tries to find the variable AK_y, which does not exist.

How can I get the value of y and access the correct variable? For example if y=2015, then access AK_2015 and (if necessary) AK_2014, if y=2018, access AK_2018 and (if necessary) AK_2017 and so on...

Thank you in advance for any help.

Smiley Happy

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Arrays are probably the easiest.  I would suggest that you remove the 201x from your variable names and just use a sequential number with the year in the label, it will make your life easier and your code look better.

data want;

     set have;

     array ak{6} 8. ak_2014-ak2019;

     y=year(input_date);

     result=ak{y-2013};

run;

Now I haven't checked the above runs as I don't have test data, but what it does is take 2013 off your year value to leave 1-9 and use that to reference the specific variable in the array.  The reason I mention sequential numbers is that if you have a date with 2013 then your going to have problems.  If its just 1-9 and 1 references the first date in the row, then the code would work in all cases, and you then just need to check the label for ak_1 to get the year.

mohamed_zaki
Barite | Level 11

data have;

input a_2014 a_2013 ;

cards;

1 4

0 7

5 8

;

run;

%let YEAR = 2014;

%let PYEAR =  %sysfunc(putn(&YEAR-1,4.));

data want;

set have;

if a_&YEAR > 0 then result1=a_&YEAR;

Else if a_&PYEAR > 0 then result1=a_&PYEAR;

run;

Ksharp
Super User

Is input_date a simple value outside the dataset  OR it is a variable include in the dataset ?

Xia Keshan

andreas_schmitz
Calcite | Level 5

input_date is a variable the user has to input via prompt in SAS.

Ksharp
Super User

So you are using EG . better post is at  SAS Enterprise Guide.

And mohamoed's code might be what you are looking for .

andreas_schmitz
Calcite | Level 5

Ok, I'll do this next time.

Ok, but I also liked the suggestion of RW9 and used an array since I don't have values before 2014.

Thank you all guys for your help. You are simply awesome.

mohamed_zaki
Barite | Level 11

Then, please mark the question as answered.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 714 views
  • 6 likes
  • 4 in conversation