Help using Base SAS procedures

Data step with multiple data sets

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Data step with multiple data sets

I am bringing an Excel file,with two worksheets, into SAS 9.2.  I want each worksheet to be a data set.  I add labels and informats and formats and everything works fine doing two data steps.  Out of curiosity, is there a way to set labels, informats and formats for two different data sets in the same data step?  In other words, can you reference a specific data sets' attributes within a given data step?

Thanks,

Adrian.


Accepted Solutions
Solution
‎12-21-2011 06:16 PM
PROC Star
Posts: 7,363

Data step with multiple data sets

Why not?  If you are using the libname method to identify the files, and bringing them in with set statements, you can always define the two (or more) desired output files in the data statement, grab the filenames by using the indsname option in the set statements to grab the filenames (see: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000173782.htm ), and then just include an if then statement to route the input records to their desired output files.

It would be especially handy if all of the input files needed the same formats and labels, etc. to be defined the same way.

View solution in original post


All Replies
Solution
‎12-21-2011 06:16 PM
PROC Star
Posts: 7,363

Data step with multiple data sets

Why not?  If you are using the libname method to identify the files, and bringing them in with set statements, you can always define the two (or more) desired output files in the data statement, grab the filenames by using the indsname option in the set statements to grab the filenames (see: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000173782.htm ), and then just include an if then statement to route the input records to their desired output files.

It would be especially handy if all of the input files needed the same formats and labels, etc. to be defined the same way.

Regular Contributor
Posts: 184

Data step with multiple data sets

Don't use a DATA step. Use PROC DATASETS instead for such metadata tasks.

PROC Star
Posts: 7,363

Data step with multiple data sets

Howard and KSharp,

If the files already existed as SAS files I'd agree.  But, in this case since the files need to be processed anyhow and, knowing Excel files probably will need some manipulation on top of the metadata, why do you both say NOT to use a datastep?  Just curious.

Super User
Posts: 9,682

Data step with multiple data sets

Art .

Ou? It looks like I don't understand what OP means exactly.

But you can always change the label or format after importing data .

You see?

Ksharp

Super User
Posts: 9,682

Data step with multiple data sets

Yes. But you need to use proc datasets - a tool of managing datasets.

data a;set sashelp.class;run;

proc datasets library=work nolist;

modify a;

attrib name label='N' format=$10. ;

quit;

Ksharp

Super User
Super User
Posts: 6,502

Re: Data step with multiple data sets

You can create two datasets with one DATA step, so yes you can assign labels and formats to two datasets at once.

But unless the variables in the two Excel sheets are the same you will have other work to do.

There are other methods that can used to make sure attributes of variables are defined consistently across datasets.

One method is to store the statements into a macro variable and then use that in two different places.

%let attrib=%str(

   attrib id length=$10 label='Patient Id';

   attrib sex length=$1 label='Gender (M/F)';

);

data one;

  &attrib

  set excel.sheet1 ;

run;

data two;

  &attrib

  set excel.sheet2;

run;

Other is to reference an existing dataset that has the variables defined.

data two ;

  if 0 then set one;

  set excel.sheet2;

run;

However if you are using Excel as a data source you will need to check it carefully. Make sure the columns actually contain consistent values that can be treated as a variable in a dataset because Excel is designed for spread sheet calculations and any cell could contain any type of data.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 149 views
  • 0 likes
  • 5 in conversation