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

I have the following sample dataset, here 99 is a missing entry.

 

DATA HAVE;
input var2001 var2002 var2003 var2004;
cards;
1 1 99 99
3 99 4 4
99 4 3 99
4 4 99 4
99 99 99 99
;
run;

I want to create a new variable 'var_cur' for latest non missing entry. If a variable only has missing entries then the new variables must have '99' as entry.

Desired output

Obs var_cur

1          1

2           4

3           3
4           4

5           99

 

Thank you for the help.

1 ACCEPTED SOLUTION

Accepted Solutions
blueskyxyz
Lapis Lazuli | Level 10
data want;
	set have;
	array a(4) var2001 var2002 var2003 var2004;
	do i=1 to dim(a);
		if a(i)^=99 then var_cur=a(i);
	end;
/*	for all missing vars*/
	if var_cur=. then var_cur=99;
run;

View solution in original post

1 REPLY 1
blueskyxyz
Lapis Lazuli | Level 10
data want;
	set have;
	array a(4) var2001 var2002 var2003 var2004;
	do i=1 to dim(a);
		if a(i)^=99 then var_cur=a(i);
	end;
/*	for all missing vars*/
	if var_cur=. then var_cur=99;
run;

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
  • 1 reply
  • 324 views
  • 1 like
  • 2 in conversation