BookmarkSubscribeRSS Feed
Satori
Quartz | Level 8

My code:

 

proc contents data=have out=list(keep=name) noprint;
data list1; set list(keep=name); if name in ('ID','Name','Code','Country','Type','Form','Status','Entity'); keep name;
data list2; set list; if substr(name, length(name)-3) = 'YEAR' then output; keep name;

data l2; set list1 list2;

proc sql noprint; select name into :l2 separated by ' ' from l2; quit;

data want_YEAR(keep=&l2); set have;

proc sql noprint; select catx("=", name, substr(name, 1, length(name)-5)) into :rename_list separated by " "
	from sashelp.vcolumn where libname='WORK' and memname='HAVE_YEAR' and upper(trim(name)) like '%_YEAR'; quit;
proc datasets library=work nodetails nolist; modify have_YEAR; rename &rename_list; run; quit;

data final; set have_YEAR;

I have ran this code by replacing YEAR with the years I want and repeat the code for as many years as I needed. Now I need to run it over quite a few number of years so I need to automate it. How can I do this?

1 REPLY 1
Kurt_Bremser
Super User

Don't keep data (years) in structure (variable names). Also see Maxim 19.

Transpose the year... variables to a long dataset and extract tze year number from _NAME_. Everything will then turn into a simple WHERE or BY.

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 365 views
  • 0 likes
  • 2 in conversation