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

I have a dataset like this, how to access the each value in the last observation? Let's say, I want the last values for the varaibles var2, var3 and var4 whose values are  3, 7.5, 8. I know that we cannot use the last. beacuse my data should not be sorted or grouped. Please help.

Obs    var1        var2       var3    var4
1016.09
45887
36.8.011
4549.012
655.10
3537.58
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

....Feel free to start there next time.

 

Use PROC STDIZE instead, with the MISSING= and REPONLY options you can replace missing values with a variety of different options, 0, mean, median, etc.

See the documentation for reference.

 

proc stdize data=have out=want reponly  missing=mean;
var var2-var5;
run;

View solution in original post

10 REPLIES 10
Reeza
Super User

What are you planning to do with said 'last' observation? Is the only way to identify this observation is that its the last record?

 

You could merge the data with itself, but keep only the last row in the second dataset. This is only one method, it depends on what you're trying to do. 

UPEN
Obsidian | Level 7

My goal is to replace the missing values with last value of that record, so I need to access the last values

Astounding
PROC Star

What if the last observation has a missing value for VAR3?  Do you want to go back to the next-to-last

 observation in that case?

UPEN
Obsidian | Level 7

My last observation wont be having missing values. Even its missing I want to replace with the missing itself or leave.

 

Astounding
PROC Star

OK, here's a generic way:

 

data want;

if _n_=1 then set have nobs=_nobs_ point=_nobs_ (rename=(var1-var4 = replacement_1 - replacement_4));

set have;

*** Add any statements you want here to change VAR1 - VAR4 based on replacement variables;

run;

Reeza
Super User

You can use this method to merge the record in. You can use COALESCE() to replace a value with antoher if missing.

If you don't know the number of obs, you can pre calculate it using a few different options.

 

data have;
input var1-var5;
cards;
1	0	1	6.0	9
4	5	8	8	7
3	6	.	8.0	11
4	5	4	9.0	12
6	5	5	.	10
3	5	3	7.5	8
;
run;

data want;
set have;
if _n_ = 1 then do;
set have (firstobs=6 rename=var1-var5 = nvar1-nvar5);
end;
run;
UPEN
Obsidian | Level 7

I appended the last to row to the original dataset, so I dont want to do a set statement again to redo it

UPEN
Obsidian | Level 7

The actual question is... I have a dataset with misssing values, now I have to replace the missing values in each colums with the average of its respective column.

My appraoch - I took the mean values in to the variables using proc means and then appended that row to the original dataset, I did this beacause if there are 200 variables I thought this would be easy. But I think this is not working.

 

Please tell me how would you achieve this?

Reeza
Super User

....Feel free to start there next time.

 

Use PROC STDIZE instead, with the MISSING= and REPONLY options you can replace missing values with a variety of different options, 0, mean, median, etc.

See the documentation for reference.

 

proc stdize data=have out=want reponly  missing=mean;
var var2-var5;
run;

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
  • 10 replies
  • 1162 views
  • 1 like
  • 3 in conversation