create variable from dataset name

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

create variable from dataset name

I would like to create a variable that contains the dataset name that each observation came from.

For the example below, each of the 'inputset' sets contain two observations.  What goes in the blank?

 

data work.new_dataset;

     set work.inputset1

          work.inputset2

          work.inputset3;

     dataset_name = ______________;

run;


OBS     ...     dataset_name

1          ...     inputset1

2          ...     inputset1

3          ...     inputset2

4          ...     inputset2

5          ...     inputset3

6          ...     inputset3


Accepted Solutions
Solution
‎08-29-2013 06:30 AM
Super Contributor
Posts: 297

Re: create variable from dataset name

A slight change is needed to Reeza's code.  INDSNAME is a data step option that needs to appear before the semi colon in the set statement.

DATA WORK.NEW_DATASET;

     SET WORK.HAVE1

          WORK.HAVE2

          WORK.HAVE3 INDSNAME=SOURCE;

DATASET_NAME=SOURCE;

RUN;

This method is only available for versions 9.2 onwards.

View solution in original post


All Replies
Super User
Posts: 17,829

Re: create variable from dataset name

Close but no cigar Smiley Happy


data work.new_dataset;

     set work.inputset1

          work.inputset2

          work.inputset3;

     indsname=source;

dataset_name=source;

run;

Solution
‎08-29-2013 06:30 AM
Super Contributor
Posts: 297

Re: create variable from dataset name

A slight change is needed to Reeza's code.  INDSNAME is a data step option that needs to appear before the semi colon in the set statement.

DATA WORK.NEW_DATASET;

     SET WORK.HAVE1

          WORK.HAVE2

          WORK.HAVE3 INDSNAME=SOURCE;

DATASET_NAME=SOURCE;

RUN;

This method is only available for versions 9.2 onwards.

Trusted Advisor
Posts: 1,128

Re: create variable from dataset name

this is a untested code, hope this works

data work.new_dataset;

     set work.inputset1(in=inputset1)

          work.inputset2(in=inputset2)

          work.inputset3(in=inputset3);

   if  inputset1 then dataset_name = "inputset1";

   else

   if  inputset2 then dataset_name = "inputset2";

   else

   if  inputset3 then dataset_name = "inputset3";

run;

Thanks,

Jagadish

Thanks,
Jag
Contributor
Posts: 20

Re: create variable from dataset name

The code that both Scott_Mitchell/Reeza and Jagadishkatam provided correctly do what I'm trying to accomplish; however, the INDSNAME=SOURCE method is a little more clean.  Thank you all for your answers! -Lee

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 228 views
  • 9 likes
  • 4 in conversation