0

I am working with the following dataset

data have;
    input repricingdate1-repricingdate3;
    datalines;

    '30SEP2019'd '31DEC2020'd '31MAR2022'd
    '31DEC2020'd '30JUN2023'd '31DEC2025'd
    ;

run;

please excuse me if that's not the correct way to input dates, I'm unsure but I have a table with those values using an intnx function

I am looking to create a variable "Flag" which returns a '1' if the repricingdate matches the Flag year i.e. Flag(2019)=1 for the first line, Flag(2020)=1 for both lines and so on.

I am using the below code and can't see where my mistake lies, it even picks up a couple of rows in a larger dataset but is sporadic

data want;
    set have;

    array flag(2018:2021) flag2018-flag2021;
    array repricingdate(1:3);

    do i = 2018 to 2021;

        do j = 1 to 3;
            if put(repricingdate(j), 4.) = compress(vname(flag(i)),, 'kd') 
            then flag(i)=1;
        end;
    end;

    drop i;

run;

I would appreciate it if someone could point out my mistake, thank you.

78282219
  • 593
  • 5
  • 21

1 Answers1

0

I don't know where your error is but you can drastically simplify this by finding the year and just setting those to 1 rather than looping through all.

data want;
    set have;

    array flag(2018:2021) flag2018-flag2021;
    array _date(1:3);

        *set all to 0 initially;
        do i=lbound(flag) to hbound(flag);
                flag(i)=0;
        end;

        do i = 1 to 3;
            year_data=year(_date(i));
            flag(year)=1;
        end;


    drop i;

run;

EDIT: Your error is here: put(repricingdate(j), 4.) -> This does not generate a year value. If you want the year use the YEAR function instead year(repricingdate(j))

This assumes your dates are actually SAS dates. As you mentioned, your demo code

Reeza
  • 20,510
  • 4
  • 21
  • 38
  • My error is that the flags aren't filling out with my second piece of code but I've managed to restructure it and get a solution. I'll try this too. – 78282219 Sep 12 '18 at 15:20
  • I added what your issue is but didn't test, as you suggested, your example code does not run correctly. There is code here to create demo data: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712 – Reeza Sep 12 '18 at 15:34