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!
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;
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;
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.
set staff point=inobs nobs=nobs;
What does the nobs=nobs do here?
is nobs (right) an automatic variable?
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;
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
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!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.