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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Mar1ene
Obsidian | Level 7

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=nobsCreates 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 0Causes 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

View solution in original post

8 REPLIES 8
Reeza
Super User
Looks like it’s trying to transpose the data by ID. You could probably do this with a PROC TRANSPOSE. This code is somewhat inefficient, since BY group processing would simplify some of it.
knagarkar
Calcite | Level 5

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.

Mar1ene
Obsidian | Level 7

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=nobsCreates 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 0Causes 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

knagarkar
Calcite | Level 5

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) 

Mar1ene
Obsidian | Level 7

Hi Knagarkar,

 

Check the below out... I think it explains it nicely...

 

https://communities.sas.com/t5/SAS-Programming/What-is-the-purpose-of-N-syntax-in-the-codes/m-p/2453...

 

Marlene

ballardw
Super User

@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.

Mar1ene
Obsidian | Level 7
Thank you Knagarkar 😄
Mar1ene
Obsidian | Level 7
Oh gosh... meant thank you Ballardw (blushing face)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 2579 views
  • 7 likes
  • 4 in conversation