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

 

data customer ;                                      
  input custid:$8;                   
  cards ;                                        
ABC123                                          
DEF456                                      
GHI789                                     
JKL101                                      
MNO112                                      
F000131                                     
G000415                                     
run ;                                            
                                                 
data staff ;                                     
  input staffname:$8;                     
  cards ;                                        
JOHN                                        
PAUL                                      
SARTRE                                
run ;

data want;
input custid:$8 staffname:$8;
 cards;
ABC123           JOHN                               
DEF456           PAUL                               
GHI789            SARTRE                       
JKL101            JOHN                        
MNO112          PAUL                        
F000131          SARTRE                      
G000415         JOHN
run;

 

 

Any quick way to do so? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

One idea: use the point= option on the second dataset:

data customer;
input custid :$8.;
cards;
ABC123
DEF456
GHI789
JKL101
MNO112
F000131
G000415
;
run;
                                                 
data staff;
input staffname :$8.;
cards;
JOHN
PAUL
SARTRE
;
run;

data want;
set customer;
if _n_ = 1 then inobs = 1;
set staff point=inobs nobs=nobs;
inobs + 1;
if inobs > nobs then inobs = 1;
run;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

One idea: use the point= option on the second dataset:

data customer;
input custid :$8.;
cards;
ABC123
DEF456
GHI789
JKL101
MNO112
F000131
G000415
;
run;
                                                 
data staff;
input staffname :$8.;
cards;
JOHN
PAUL
SARTRE
;
run;

data want;
set customer;
if _n_ = 1 then inobs = 1;
set staff point=inobs nobs=nobs;
inobs + 1;
if inobs > nobs then inobs = 1;
run;
t30
Fluorite | Level 6 t30
Fluorite | Level 6
That's awesome. Although I've to admit, it looks pretty cryptic ^^. I'll have a read further on this option.

Thanks!
Kurt_Bremser
Super User

Both the point= and the nobs= option define variables that are not included in any output datasets.

The nobs= variable is set to the number of observations in the input dataset when the data step starts executing.

The point= variable is used to read a selected observation from the input dataset.

 

So I set inobs in the first data step iteration (_n_ = 1) to 1, and increment it after reading (using inobs + 1; instead of inobs = inobs + 1; automatically retains it). If it passed the number of observations available, I reset it to 1.

t30
Fluorite | Level 6 t30
Fluorite | Level 6
set staff point=inobs nobs=nobs;

What does the nobs=nobs do here?

is nobs (right) an automatic variable? 

Kurt_Bremser
Super User

It's not an automatic variable (like _N_ or _ERROR_), but one that comes into existence because it is named in the nobs= option.

It is not included in outputs (it is implicitly dropped), and set once when the data step starts executing; if you re-set it, it loses its value:

data _null_;
set sashelp.class nobs=nobs;
if _n_ = 3 then nobs = 2;
put nobs=;
run;
Kurt_Bremser
Super User

PS do testruns of your code before you post it. As originally posted, your example datasets look quite funny:

data customer ;                                      
  input custid:$8;                   
  cards ;                                        
ABC123                                          
DEF456                                      
GHI789                                     
JKL101                                      
MNO112                                      
F000131                                     
G000415                                     
run ;                                            
                                                 
proc print data=customer;
run;

data staff ;                                     
  input staffname:$8;                     
  cards ;                                        
JOHN                                        
PAUL                                      
SARTRE                                
run ;

proc print data=staff;
run;

Result:

  Obs    custid

   1       D   
   2       J   
   3       F   
                 

 Obs    staffname

  1         P    
t30
Fluorite | Level 6 t30
Fluorite | Level 6
Thanks, will note this next time ^^

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2090 views
  • 1 like
  • 2 in conversation