Dummy Data:
MEMNAME | _var1 | var2 | var3 | var4 |
XY | XYSTDAT_1 | XYENDAT_2 | XYXYDDAT_3 | XYXYDAT_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!
@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;
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.
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
@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;
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.
@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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.