DATA Step, Macro, Functions and more

Proc Import and Data steps - Macro or Loop?

Reply
Senior User
Posts: 1

Proc Import and Data steps - Macro or Loop?

Hello there Folks.

First time using this Forum here.

 

I'll try to be conscise regarding my problem.

 

Datasets types - 20 climate model projections for temp max and min, rain, wind and radiation = 100 csv 

 

Problem - Import and managing this dataset into SAS

 

Question - Well, I'd like to learn how I can optimize this process of importing of these 20 climate model outputs for each of my 5 variables.

For example, regarding the PROC IMPORT. How I can use Macros or Loop or Do statement in order to with one command, import and create all the files? For each variable, I'd like to run my macros and perform 20 importations.

 

After this, if I want to do a operation (using Data statement), like creating a variable to distingh the different datasets I've created, I can do by a loop right? Example below. How is possible to optimize this process, in order to avoid a complicated and long script?


 

Data model1_precipitation;

  set model1_precipitation;

  model_number=1;

run;

 

Any tips regarding this problem is really appreciate. 

Thanks!

Super User
Posts: 11,343

Re: Proc Import and Data steps - Macro or Loop?

If dealing with multiple data files of similar layout especialy in a text format such as CSV a data step is likely to best in the long run. One reason would be that the code is more reuseable when you do the next set of runs. Also proc import, depending on the values may generate data sets with one or more variables as character in from one source file and numeric from another. The data step allows you to specify.

 

There are methods that allow reading multiple data files using wildcards and then having the name of the input file available as a variable in the data set.

We could use more information to provide a more specific response. Such as do the model projections have column headers? Naming convention?

 

If your data doesn't have column headers you could have code that starts as simply as

filename source "c:\path\*.csv"; /* reads all csv extension files in the folder identified by path*/

 

data raw;

   infile source <various options> filename=Source;

   input maxtemp mintemp precip wind radiation;

   length InputFileName $ 200;

   InputFileName=source;

run;

 

I recommend label and format assignments.

 

If it seems that you have a separate file for each variable then we have other bits to straighten out (such as who writes there models that way) to align your data.

You do need to provide some more examples of what you actually have.

Ask a Question
Discussion stats
  • 1 reply
  • 357 views
  • 0 likes
  • 2 in conversation