BookmarkSubscribeRSS Feed
dwilliams1
Calcite | Level 5

 

I am trying to add a column to a data table that was just created.  Prior to this I've connected to a database and extracted the files of interest.  The issue is that the lot ID is only contained in the file name and no where in the table itself. This will be an issue when I concatenate there files as I'll have no way distinguishing individual lots.  So, I'm trying to create a new column containing the lot id which is the value contained in ParamList for that iteration of the macro.  FYI, the sql step works fine. Any suggestions would be greatly appreciated.   Thank you!

 

 

options symbolgen mprint;

%macro loop;
 %do i=1 %to 10;
  proc sql;
   create table &&ParamList&i.._data as
   select *
   from Prod.&&ParamList&i.._DATA;
  quit;

  data &&ParamList&i.._data;
   set &&ParamList&i.._data;
   Run=%str(%'&&ParamList&i%');  /*  Here's where I get into trouble  */
 %end;
%mend;

%loop;

 

The following works fine but is completely manual and not realistic given the quantity I need to pull.

 

data xyz_data_data;
 set xyz_data;
 runs='xyz';
run;

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

That really is not a good way to model data, or to program, hence why you are having issues.  Do not create many datasets with the same data in to start with, this will just vastly increase the amount of programming needed and make your code slower and harder to maintain.  A good clue, if your having to do macro loops, then its likely your data is badly modelled.

A simple fix:

data param;
length dsname $200; set prod.: indsname=tmp; dsname=tmp; run;

With this it will set all datasets in prod together, and create a variable called dsname. You can now process all the data in one step, no loops or other code needed.  As I do not see what paramlist refers to I can't be more specific.

 

Astounding
PROC Star

I would replace this statement:

 

Run=%str(%'&&ParamList&i%');  /*  Here's where I get into trouble  */

 

Here's a better way:

 

Run="&&ParamList&i";

 

Be sure to use double-quotes, not single quotes.

 

In context, you might want to set a length for RUN, since it would be inconvenient to have it change length from one data set to the next.  So the program becomes:

 

data &&ParamList&i.._data;
   set &&ParamList&i.._data;
   length run $ 50;
   run = "&&ParamList&i";
run;

It's not clear if you want the new variable to be named RUN or RUNS, but that's easy to change if you want to. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 2310 views
  • 0 likes
  • 3 in conversation