BookmarkSubscribeRSS Feed
joycefilm
Calcite | Level 5

The following datasets temp1 and temp2. What methods are available to get temp3?

数据集temp1:

Visit    tpt   VAL

C1D1   -12    1

C2D1    0     2

C3D1    1     4

C4D1    2     5

C5D1    3     1

...   

 

数据集temp2:

 VAL=3

 

数据集temp3:

Visit    tpt   VAL

C1D1   -12    1

C1D1   -12    3

C2D1    0     2

C2D1    0     3

C3D1    1     4

C3D1    1     3

C4D1    2     5

C4D1    2     3

C5D1    3     1

C5D1    3     3

 

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @joycefilm,

 

You can read dataset TEMP2 in each iteration of a DATA step reading dataset TEMP1 by using the POINT= option (see documentation of SET options):

data temp3;
set temp1;
output;
_n_=1;
set temp2 point=_n_;
output;
run;

 

To generalize this to a TEMP2 dataset with two or more observations, use a DO loop and the NOBS= option:

data temp3;
set temp1;
output;
do _n_=1 to n2;
  set temp2 point=_n_ nobs=n2;
  output;
end;
run;

 

 

Tom
Super User Tom
Super User

Do you know how TEMP3 is supposed to be created from TEMP1 and TEMP2, but just don't know how to program the algorithm? 

 

If so can you explain how TEMP3 is related to TEMP1 and TEMP2?

 

Or are you expecting use to GUESS how TEMP3 is related to TEMP1 and TEMP2?

 

 

Reeza
Super User
You can hard code in the duplication with Val = 3, but if that will change and you need multiple or something it will be a different process. And is TEMP2 a single variable with the val = 3 or does it have a variable called VAL set to 3?
mkeintz
PROC Star

Let's generalize a little, and assume that TEMP2 has more than one observation, and has more than one variable, some of which may not be in TEMP1.

 

 

data temp1;
  input Visit :$4.   tpt   VAL;
datalines;
C1D1   -12    1
C2D1    0     2
C3D1    1     4
C4D1    2     5
C5D1    3     1
run;

data temp2;
  input val   newvar;
datalines;
3     1001
3.1   1002
run;

 

 

Then it might be worthwhile to make a hash of TEMP2 (with an additional _ORDER variable).  For each obs in TEMP1, output to temp3 and add an extra obs for every dataitem in the hash, in sequence determined by _ORDER.

 

data dummy2;
  _order=_n_;
  set temp2;
run;

data temp3 (drop=_:) ;
  set temp1;
  output;

  if _n_=1 then do;
    if 0 then set dummy2 nobs=size_of_temp2;
    declare hash h (dataset:'dummy2');
      h.definekey('_order');
      h.definedata(all:'Y');
      h.definedone();
  end;

  do _order=1 to size_of_temp2;
    h.find(key:_order);
    output;
  end;
  call missing(of _all_);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

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.

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
  • 4 replies
  • 277 views
  • 0 likes
  • 5 in conversation