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

Hello everyone,

 

I have been looking through this Forum a lot and really appreciate all the solutions I was able to derive from here.

 

However now I'm having a problem I've been struggling with for the last two days. This is my first post about SAS so I might not be the best in explaining my problem.

 

So i have a dataset that is something like the following:

 

ID    year      EQ       flag

1      1900      5           0

1      1901      3           0

1      1902      1           0

2      2000     -1         2000

2      2001      1           0

3      1970      -1        1970

3      1971      0           0

 

alright so now I want to get a new coloum "Help" that copies the last flag value like the following:

 

ID    year      EQ       flag        HELP

1      1900      5           0             0

1      1901      3           0             0

1      1902      1           0             0

2      2000     -1         2000      2000

2      2001      1           0          2000

2      2002      1           0          2000

2      2003      1           0          2000

3      1970      -1        1970      1970

3      1971      0           0          1970

 

If've been trying to use the ifn function in combination with the Lag function to do this.

 

1)    HELP= ifn(flag, flag, LAG(Help))

 

I tried the following as well

 

2)    HELP= ifn(flag=0, LAG(Help), flag)

 

1) However is only copying the Help value for one following row after that it is zero again. It gives me a result like:

ID    year      EQ       flag        HELP

1      1900      5           0             0

1      1901      3           0             0

1      1902      1           0             0

2      2000     -1         2000      2000

2      2001      1           0          2000

2      2002      1           0             0

2      2003      1           0             0

3      1970      -1        1970      1970

3      1971      0           0          1970

 

 

 

2) Works a little better but only copies the value for the next two rows and after that gives me zero again:

 

ID    year      EQ       flag        HELP

1      1900      5           0             0

1      1901      3           0             0

1      1902      1           0             0

2      2000     -1         2000      2000

2      2001      1           0          2000

2      2002      1           0          2000

2      2003      1           0             0

3      1970      -1        1970      1970

3      1971      0           0          1970

 

 

I don't really understand why the ifn is stopping to execute. I hope someone can explain me why this is happening or has a solution to my problem.

 

Hopefully I expressed my problem precisely enough.

 

Thanks in advance for all tips.

 

Best regards

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Once you see it, I suspect it makes a lot of sense:

 

data want;

set have;

by id;

retain help;

if first.id or flag ne 0 then help = flag;

run;

View solution in original post

4 REPLIES 4
Nici
Obsidian | Level 7

Sorry I just noticed I confused the solutions.

 

The result I posted for 1) is actually the one for 2) and the one for 2) is the one for 1). It shouldn't be much of a problem.

Astounding
PROC Star

Once you see it, I suspect it makes a lot of sense:

 

data want;

set have;

by id;

retain help;

if first.id or flag ne 0 then help = flag;

run;

Nici
Obsidian | Level 7

Thank you for your very fast reply. Your code is intuitive and I understand the logic behind it.

 

However if I use this code SAS basically doesn't do anything, why is that?

 

I receive:

 

ID    year      EQ       flag        HELP

1      1900      5           0             0

1      1901      3           0             0

1      1902      1           0             0

2      2000     -1         2000      2000

2      2001      1           0             0

2      2002      1           0             0

2      2003      1           0             0

3      1970      -1        1970      1970

3      1971      0           0             0

 

 

Nici
Obsidian | Level 7

Ok, I got it to work now.

 

Thank you so much, you saved my life. ❤️

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 3210 views
  • 1 like
  • 2 in conversation