I got a dataset like this and wodering how to fillin the missing values. Like for each id should have 3 visits, but few of them only have one or two, I need to let them have 3 visits with missing values replaced by .
For examplem, the id 3 only have one visit 1 and I need to set its visit 2 and 3 with a ".".
Set exactly which variable(s) with missing?
Assuming the only variable involved that needs to be set missing would be score then perhaps: (untested code as very hard to code against a picture)
data want; set have; by id; output; if last.id and visit ne 3 then do visit=(visit+1) to 3; call missing(score); /* if you have other variables that should be missing add them to the call missing, separate the names with a comma */ output; end;
run;
The first output writes the incoming record to the output data set. The BY statement creates automatic variables indicating whether the current record is the First or Last of the by group and is a 1 (true) or 0 (false) value. Note the dot between the keyword and the variable name. So if the last ID has visit that is not 3 then we do a loop to write the additional visits with missing values for the variables in the Call missing () function. Call missing is one of the few functions that can take both character and numeric variables so just add the names as needed.
Thank you for your answer. But when I run this code it is a error "A loop variable cannot be an array name or a character variable; It must be a scalar numeric" under visit = (visit +1) to 3.
@tjzzzz wrote:
Thank you for your answer. But when I run this code it is a error "A loop variable cannot be an array name or a character variable; It must be a scalar numeric" under visit = (visit +1) to 3.
Show the code that you ran with the messages from the log. Copy the entire data step from the LOG.
Paste into a code box opened on the forum with the </> icon to preserve formatting of the diagnostic characters.
As I said, the code was not tested because you did not provide data in a usable form and we can't code against pictures. I cannot tell if your "visit" variable is character or numeric. Visit numbers, being something I do arithmetic with, are usually that, numbers. If yours are charter then you need to provide data.
I did not include any array code so if you included some that is critical to know.
I have this dataset
I want a dataset with missing values be fillined like this. For example for id 3, the visit only have 1 and i need two new lines for visit 2 and visit 3 with score be a dot.
So, is the real question you are asking that you want to know how to make sure there are exactly three records (never less than three, never more than three) for each ID, with visit numbers 1 2 and 3? AND why do you need this? What can you do if you have those extra records that you can't do otherwise?
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.
Ready to level-up your skills? Choose your own adventure.