data vite_b (keep=SID strata1 plaque1 plaque2 trt);
set vite nobs=nobs;
retain SID 0 strata1 0 plaque1 0 plaque2 0 trt 0;
if _N_ EQ 1 then do;
sid = ID;
strata1 = ID;
trt = treatment;
end;
if SID NE ID then do;
output;
SID = ID;
strata1 = strata;
trt = treatment;
end;
if visit = 0 then plaque1= plaque;
if visit = 2 then plaque2=plaque;
if _N_ = nobs then output;
run;
I am new learner of SAS and I encountered issue understanding this chunk of code, if anyone of you could please explain each line of the code as in what's its doing then it will be of very help.
Also whats the use of certain statement in this code (nobs, _N_)
Again Thanks for your time in advance.
Hi Knagarkar,
Let me try with my limited knowledge... lets learn together 😄
data vite_b data output name (keep=SID strata1 plaque1 plaque2 trt); only keeping the specified variables in the output table
set vite data intput table
nobs=nobs; Creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets.
retain SID 0 strata1 0 plaque1 0 plaque2 0 trt 0; Causes a variable that is created by an INPUT or assignment statement to retain its value from one iteration of the DATA step to the next.
if _N_ EQ (equal to) 1 then do; condition to be met for below 'instructions' i.e. 'then do'
sid = ID;
strata1 = ID;
trt = treatment;
end; this ends the 'then do' statement
if SID NE (not equal to) ID then do; once again a condition for the then do instruction
output; write to the table vite_b as specified above
SID = ID;
strata1 = strata;
trt = treatment;
end; ends the 'then do' statement
if visit = 0 condition then plaque1= plaque instruction;
if visit = 2 condition then plaque2=plaque instruction;
if _N_ = nobs condition then output instruction;
run;
LOL!! I hope this helps
Marlene
Thanks Reeza for your response,
I would probably go for transpose only but just wanted to understand few new statement's (for me) in this code.
Hi Knagarkar,
Let me try with my limited knowledge... lets learn together 😄
data vite_b data output name (keep=SID strata1 plaque1 plaque2 trt); only keeping the specified variables in the output table
set vite data intput table
nobs=nobs; Creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets.
retain SID 0 strata1 0 plaque1 0 plaque2 0 trt 0; Causes a variable that is created by an INPUT or assignment statement to retain its value from one iteration of the DATA step to the next.
if _N_ EQ (equal to) 1 then do; condition to be met for below 'instructions' i.e. 'then do'
sid = ID;
strata1 = ID;
trt = treatment;
end; this ends the 'then do' statement
if SID NE (not equal to) ID then do; once again a condition for the then do instruction
output; write to the table vite_b as specified above
SID = ID;
strata1 = strata;
trt = treatment;
end; ends the 'then do' statement
if visit = 0 condition then plaque1= plaque instruction;
if visit = 2 condition then plaque2=plaque instruction;
if _N_ = nobs condition then output instruction;
run;
LOL!! I hope this helps
Marlene
Hey Mar1ene,
That was quite elaborate 🙂
Thank you.
Also I would want to understand few statements in the below code:
1) if _N_ EQ (equal to) 1 then do; (What is _N_, also does it mean that this part will run only once)
2) What if I don't assign '0' to all these variables: retain SID 0 strata1 0 plaque1 0 plaque2 0 trt 0;
3)
Hi Knagarkar,
Check the below out... I think it explains it nicely...
Marlene
@knagarkar wrote:
Hey Mar1ene,
That was quite elaborate 🙂
Thank you.
Also I would want to understand few statements in the below code:
1) if _N_ EQ (equal to) 1 then do; (What is _N_, also does it mean that this part will run only once)
2) What if I don't assign '0' to all these variables: retain SID 0 strata1 0 plaque1 0 plaque2 0 trt 0;
3)
_N_ is an automatic variable SAS creates that indicates the iteration number of a data step. If you have a simple program as above you might think of it as the number of records read from in data set (s) on the set statement.
If you don't assign a value to a retained variable it will be created as missing. If you have a list of variables on retain such as
Retain A B C 0; then all of A B and C will initialize as 0.
Take care with retained Character variables. You might set the length of the variable in the retain statement to be too short to hold desired values. Provide a length statement before the retain to ensure that your character variables are long enough to use.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.