Hi,
I want to create a global variable with part of its variable name based on values out of a list of variable names in a dataset. In this case, a global variable, max2020, based on the max values of the last 4 letters of variables c2018,c2019,c2029.
Any help is appreciated.
data have; input ID c2018 c2019 c2020;
datalines;
1 2 3 3
2 4 3 2
3 2 3 4
4 3 2 2
;
This is the code that works, based on feedback from you all. Really appreciated it. So when there is a new variable c2021 added for a new data cut, it would automatically pick up c2021 when I want to calculate maximum year.
data havex; input id 1 c2018 3-6 c2019 8-11 c2020 13-16;
datalines;
1 2018 2019 2020
2 2018 2020
3 2018 2019
4 2018
;
proc contents data=havex (keep=c:) out=toc noprint; run;
data _null_ ;
set toc end=eof;
if eof then call symputx('lastyr',substr(name,2,4));
run;
data want; set havex;
maxyear=max(of c2018-c&lastyr);
run;
Have data:
ID c2018 c2019 c2020 maxyear
1 2018 2019 2020 2020
2 2018 2020 2020
3 2018 2019 2019
4 2018 2018
What to you mean by "global variable"? Do you mean macro variable in the global symbol space?
What to you want to NAME the macro variable? What value do you want the macro variable to have?
If you want to variable to be named MAX2020 and have to last variable name in the series of variables with names like cYYYY then perhaps this simple code will do what you want?
The output of PROC CONTENTS will be sort by the NAME of the variable.
proc contents data=have(keep=c:) out=contents noprint; run;
data _null_;
set contents end=eof;
if eof then call symputx('max2020',name);
run;
Note this assumes the the cYYYY variables are the only ones that start with the letter C and that they all have the same case for the letter C so that they will sort properly.
data have;
input ID c2018 c2019 c2020;
max2020 = max(of c2018-c2020);
datalines;
1 2 3 3
2 4 3 2
3 2 3 4
4 3 2 2
;
Like that?
@Solph wrote:
Hi,
I want to create a global variable with part of its variable name based on values out of a list of variable names in a dataset. In this case, a global variable, max2020, based on the max values of the last 4 letters of variables c2018,c2019,c2029.
Any help is appreciated.
data have; input ID c2018 c2019 c2020; datalines; 1 2 3 3 2 4 3 2 3 2 3 4 4 3 2 2 ;
I wasn't clear in my question. What I actually need is grab the last four digits of variable name and find the max value (in this case 2020) and then use that max value as a value in macro parameter (&lastyear) that would let me do work such as
myyear=max(of c2018-c&lastyear).
You might ask why not just lastyear=max(c2018-c2020). That's because variables in the list are dynamic, so I need it an automatic process. Tom's solution is closer to what I want. Just need to make it a system variable.
Can you use the prefix wildcard to select just the Cnnnn variables?
data _null_;
set have;
array _c c: ;
do index=1 to dim(_c);
n=input(substr(vname(_c[index]),2),4.);
min=min(min,n);
max=max(max,n);
end;
call symputx('firstyear',min);
call symputx('lastyear',max);
run;
Or will there be a conflict with the names of other variables in your dataset, names like CASEID?
You can make it a little more bullet proof.
data _null_;
set have (keep=_numeric_);
array _c c: ;
do index=1 to dim(_c);
n=input(substr(vname(_c[index]),2),??32.);
if 1950<= n <= 2050 then do;
min=min(min,n);
max=max(max,n);
end;
end;
call symputx('firstyear',min);
call symputx('lastyear',max);
run;
This is the code that works, based on feedback from you all. Really appreciated it. So when there is a new variable c2021 added for a new data cut, it would automatically pick up c2021 when I want to calculate maximum year.
data havex; input id 1 c2018 3-6 c2019 8-11 c2020 13-16;
datalines;
1 2018 2019 2020
2 2018 2020
3 2018 2019
4 2018
;
proc contents data=havex (keep=c:) out=toc noprint; run;
data _null_ ;
set toc end=eof;
if eof then call symputx('lastyr',substr(name,2,4));
run;
data want; set havex;
maxyear=max(of c2018-c&lastyr);
run;
Have data:
ID c2018 c2019 c2020 maxyear
1 2018 2019 2020 2020
2 2018 2020 2020
3 2018 2019 2019
4 2018 2018
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.