I have table as shown below:
Sourcetable | Count_103018 | Count_110518 | Count_111218 | Count_111918 |
CRS | 19,869 | 19,924 | 20,270 | 20,233 |
CRS-Rater | 16,429 | 16,569 | 16,893 | 17,125 |
InRaterOnly | 18,662 | 18,702 | 18,691 | 19,235 |
TOTAL | 54,960 | 55,195 | 55,854 | 56,593 |
I want to create a macro which will hold the suffix of second column name. Like for example the second column name here is Count_103018, So macro should hold 103018, (second column name will change as per the week)
%let dsid=%sysfunc(open(dataset_name));
%let var_nm=%scan(%sysfunc(varname(&dsid,2)),2,'_');
%let dsid=%sysfunc(close(&dsid));
%put &=var_nm;
Do you have that "table" as a SAS dataset or a text file or some other format?
You can query the dictionary.columns for the second variable in the table.
proc sql;
select strip(scan(name,2,'_')) into :var_2
from dictionary.columns
where libname='WORK' /* Library name */
and memname='HAVE' /* Table name */
and varnum=2 /* Variable 2 */
;
quit;
%put &var_2;
%let dsid=%sysfunc(open(dataset_name));
%let var_nm=%scan(%sysfunc(varname(&dsid,2)),2,'_');
%let dsid=%sysfunc(close(&dsid));
%put &=var_nm;
Assuming that the suffix on that names is date value in MMDDYY format (who uses two digit years? Y2K anyone?).
Not sure you need any macro variables. For example you could convert that structure to a TALL structure where the date value is actually in a variable instead of trapped in the variable's name.
data want ;
set have ;
array _count count_: ;
do month_no=1 to dim(_count);
count=_count(month_no);
date = input(scan(vname(_count(month_no)),2,'_'),mmddyy8.);
output;
end;
format date yymmdd10. ;
drop count_: ;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.