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 ^^

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1358 views
  • 1 like
  • 2 in conversation