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

Is there a way to create variables like - DEC_2020 etc..from:

%macro stupid;

proc sql;

create table B as

select *

%do i=1 %to 6;

,1 as 'DEC_'||%EVAL(2019+&i.)

%end;

from A;

quit;

%mend;

The expression 'DEC_'||%EVAL(2019+&I.) is not working as expected, but is there a function or any other way where I can get this to work?

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

 I think you have to explain more clearly exactly what you want to do.

 

Does this answer your question ?

 

data have;
informat start_date anydtdte.;
input start_date var_2021 var_2022 var_2025;

value=vvaluex(cats('var_',year(start_date)));
put value=;
cards;
DEC2021 1 2 3
APR2022 4 5 6
JUN2025 7 8 9
;
run;

View solution in original post

7 REPLIES 7
Reeza
Super User

Are you required to use SQL?

A data step can be easier.

 

Otherwise, remove the quotes around Dec and the || and see if you get what you need.

 


@thepushkarsingh wrote:

Is there a way to create variables like - DEC_2020 etc..from:

%macro stupid;

proc sql;

create table B as

select *

%do i=1 %to 6;

,1 as 'DEC_'||%EVAL(2019+&i.)

%end;

from A;

quit;

%mend;

The expression 'DEC_'||%EVAL(2019+&I.) is not working as expected, but is there a function or any other way where I can get this to work?

 


 

novinosrin
Tourmaline | Level 20

"Are you required to use SQL?

A data step can be easier."

 

That's sound of a real professional who knows stuff 

thepushkarsingh
Quartz | Level 8

Apologies, I constructed a wrong example(SQL is definitely not needed).

What I intend to ask is 'is there a way(any function) to evaluate an expression, which takes value from another variable and use it as a reference to a variable?'.

Like let's say the variable start_date is populated as-

start_date

DEC2021

APR2022

JUN2025

...

And accordingly the reference changes from Variable VAR_2021 to VAR_2022 to VAR_2025 for different rows.

gamotte
Rhodochrosite | Level 12

 I think you have to explain more clearly exactly what you want to do.

 

Does this answer your question ?

 

data have;
informat start_date anydtdte.;
input start_date var_2021 var_2022 var_2025;

value=vvaluex(cats('var_',year(start_date)));
put value=;
cards;
DEC2021 1 2 3
APR2022 4 5 6
JUN2025 7 8 9
;
run;
thepushkarsingh
Quartz | Level 8

Thank you very much.

ballardw
Super User

@thepushkarsingh wrote:

Is there a way to create variables like - DEC_2020 etc..from:

%macro stupid;

proc sql;

create table B as

select *

%do i=1 %to 6;

,1 as 'DEC_'||%EVAL(2019+&i.)

%end;

from A;

quit;

%mend;

The expression 'DEC_'||%EVAL(2019+&I.) is not working as expected, but is there a function or any other way where I can get this to work?

 


One way:

%macro stupid;
   proc sql;
   create table B as
   select *
   %do i=1 %to 6;
      %let temp= %eval(2019+&i.);
   ,1 as DEC_&temp.
   %end;
   from a;
   quit;
%mend;

macro language does concatenation a bit differently so you don't need the || operator. Also variable names in SQL can't be in quotes.

I create a temporary variable so it is a bit clearer what happens with the DEC_&temp. string.

,1 as DEC_%eval(2019+&i.)

also works but can be a bit ugly AND you do not want a space before the %eval, which makes the code a tad uglier

gamotte
Rhodochrosite | Level 12

Hello,

 

You are building variable names not strings containing variable names.

Hence the quotes and the concatenation operator are superfluous.

 

%macro stupid;

	proc sql;
		create table B as
		select *
		%do i=1 %to 6;
		    %let vname=DEC_%eval(2019+&i.);
		    ,1 as &vname.
		%end;
		from sashelp.class;
	quit;

%mend;

It would be simpler with a data step (no macro necessary).

 

data C;
    set sashelp.class;
    attrib DEC_2020-DEC_2025 length=3.;
    array DEC DEC_2020-DEC_2025;

    do over DEC;
        DEC=1;
    end;
run;

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 965 views
  • 5 likes
  • 5 in conversation