BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rain_28
Calcite | Level 5

Dummy Data:

MEMNAME _var1    var2     var3var4
XY       XYSTDAT_1XYENDAT_2XYXYDDAT_3XYXYDAT_4

 

I am looking for 2 different out in 2 different macro variables within a datastep: 

1st macro variable should have XYSTDAT

2nd macro variable should have TEST_XYSTDAT

 

How do I assign value from this "strip(reverse(substr(strip(reverse(testcase(i))),3)));" to a macro variable?

1) %let x = strip(reverse(substr(strip(reverse(testcase(i))),3))); ------> will not work

2) b = strip(reverse(substr(strip(reverse(testcase(i))),3)));

     %let y = b; ----------------------> is not resolving either

3) %let z = TEST_&x.  ----> Z resolves to TEST_strip(reverse(substr(strip(reverse(testcase(i))),3)))

 

I am looking for a solution without using Call Symput (I get desired value with this), because I want to use the macro variable within the same datastep as the loop iterates.

 

Appreciate your input! Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Rain_28 wrote:

Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).

want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.

 

MEMNAME  _var1 dates
XY        XYSTDAT_1 xystdat
ab abdat_1 abdat
cd cddat_1 cddat

 

call execute('data test;
set "||strip(libname)||"."||strip(memname)||";
%DT(DAT=&x);..........

 

There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute


Now I am even more confused.  

Sounds like you have a dataset that is the set of parameter values to pass to a macro.

Let's assume you have a macro that takes as input the name of a dataset and the name of a variable.

%dt(ds=sashelp.class,var=age)
%dt(ds=sashelp.cars,var=hp)
...

And you have dataset with a list of dataset names and the corresponding variable names.

Just use the dataset to generate the macro calls so that they run AFTER the data step that generated them ends.

data _null_;
  set have;
  call execute(catx(' '
    ,'%nrstr(%dt)'
    ,'(ds=',catx('.',libname,memname)
    ,',var=',varname
    ')'
  ));
run;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

The request does not make much sense.

What do you want to do with the macro variable during data step that you could not do with a REAL variable?

If you need the macro variable to generate SAS code then by definition the macro variable has to exist before SAS begins compiling the data step.   So it cannot use any value read during the data step.

 

What is TESTCASE?  That does not appear in your example data.

Rain_28
Calcite | Level 5

Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).

want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.

 

MEMNAME _var1dates
XY       XYSTDAT_1xystdat
ababdat_1abdat
cdcddat_1cddat

 

call execute('data test;
set "||strip(libname)||"."||strip(memname)||";
%DT(DAT=&x);..........

 

There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute

Tom
Super User Tom
Super User

@Rain_28 wrote:

Memname is actually a dataset name and dates is actual date variable (that I am extracting from _var1).

want to put Dates in macro variable and call datasets with call execute. As the loop iterates want to pick memname and correspondaing dates variable from that memname.

 

MEMNAME  _var1 dates
XY        XYSTDAT_1 xystdat
ab abdat_1 abdat
cd cddat_1 cddat

 

call execute('data test;
set "||strip(libname)||"."||strip(memname)||";
%DT(DAT=&x);..........

 

There are 100s of datasets in memname and I don't know dates in each of those datasets. I am trying to identify date variable from the dataset and use those in call execute


Now I am even more confused.  

Sounds like you have a dataset that is the set of parameter values to pass to a macro.

Let's assume you have a macro that takes as input the name of a dataset and the name of a variable.

%dt(ds=sashelp.class,var=age)
%dt(ds=sashelp.cars,var=hp)
...

And you have dataset with a list of dataset names and the corresponding variable names.

Just use the dataset to generate the macro calls so that they run AFTER the data step that generated them ends.

data _null_;
  set have;
  call execute(catx(' '
    ,'%nrstr(%dt)'
    ,'(ds=',catx('.',libname,memname)
    ,',var=',varname
    ')'
  ));
run;
Rain_28
Calcite | Level 5

Yes, the code worked by writing the whole datastep in call execute...this is the first time I wrote complicated call execute within multiple loops in a macro. It took me couple of days but got it....Thank you so much! Really appreciate your help.

Tom
Super User Tom
Super User

@Rain_28 wrote:

Yes, the code worked by writing the whole datastep in call execute...this is the first time I wrote complicated call execute within multiple loops in a macro. It took me couple of days but got it....Thank you so much! Really appreciate your help.


Good.  The point of making a macro that the code generation can call is to simplify the data driven code generation step.  Rather than generating the full data step code you just need to pass the parts that vary to a macro.  Then you can debug the process of converting the parts that vary into proper code independent from the data driven step.

 

You can also skip CALL EXECUTE() and just use the data step to write the code directly to a file.  This will normally be a lot easier to debug.  Plus you can use the power of the PUT statement and not have to fight with  text manipulation function calls.  You can examine the file and make sure it will work. Copy and paste part of the code back into the editor window and run it to see if it is going to work. etc.

filename code temp;
data _null_;
  set metadata ;
  file code;
  put 'data new;'
    / '  set ' dsname ';'
    / 'run;'
  ;
run;
%include code / source2;
  
Kurt_Bremser
Super User

To keep values across data step iterations, use retained variables (or a temporary array, which is also retained).

To dynamically set and fetch macro variables, use CALL SYMPUT and SYMGET.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 909 views
  • 0 likes
  • 3 in conversation