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

You are completely right Sir. Thank you to everyone who helped.

Tom
Super User Tom
Super User

@Brian3 wrote:

its just because they are already macro variables. If there's a quick way to convert them to data values, that would be ok.


How did they get into macro variables without first being in data?

It is simple to convert macro variables to data.  If you want to treat them as numbers then you will need to convert them back from the character strings that they are.

data want;
  length index 8 mvar_name $32 value 8;
  do index=1 to 3;
    mvar_name=cats('var',index);
    value = input(symget(mvar),32.);
    output;
  end;
run;
Kurt_Bremser
Super User

@Brian3 wrote:

it is more the principle I am trying to do rather than specific data. Basically how to work with macro variables which can change value during a data step. Surely SAS has some ability to let me do that. Maybe I can use arrays, but not sure how it works with macro variables.

 

 


You are still on the completely wrong path here. The macro language is a code generator, helping you in automatically writing dynamic code, but it is the wrong tool for dealing with data.

 

So if you want to start learning the use of the macro language, you need to start with code that needs to be made dynamic to respond to certain outside conditions, not with data issues. For these you use arrays, retained variables, lag functions, look-ahead reading, hash objects, and so on.

Brian3
Obsidian | Level 7

I've tried using arrays above.

Tom
Super User Tom
Super User

Here is an ARRAY example.

First start with some data that has a series of similar variables.

 

data have ;
  input x y1-y5 ;
cards;
2 1 1 0 0 0 
1 1 2 3 4 5
;

Now you can use an ARRAY to reference the five "y" variables.

 

Perhaps like this:

 

data want;
  set have ;
  array y y1-y5;
  success = x < min(of y[*]);
run;

Or by using an index into the array.

 

 

data _null_;
  set have;
  array y y1-y5 ;
  do index=1 to dim(y);
     if x < y[index] then put x= 'is less than ' y[index]=;
  end;
run;

 

 

Brian3
Obsidian | Level 7

ok sas is useless I'll just use data values and an array.

Tom
Super User Tom
Super User

@Brian3 wrote:

ok sas is useless I'll just use data values and an array.


Exactly. Start by learning how to use SAS.  Once you understand how to do things in SAS you can then figure out how to use macro language to generate that code.  You cannot build a house without a solid foundation.

Kurt_Bremser
Super User

@Brian3 wrote:

ok sas is useless I'll just use data values and an array.


WRONG.

Neither Base SAS nor macro language is "useless", they are tools to be used correctly. You don't try to carve up your Thanksgiving turkey with a chainsaw, and you don't cut the Christmas tree with a Swiss Army Knife.

Tom
Super User Tom
Super User

Before trying to use macro code to generate SAS code first figure out want SAS code you want to run.

It sounds like you want to do the following.

 

data want;
  set have ;
  success = .Z < x < max(of VAR_2030-VAR_2032);
run;

Now if your variable names really are in that form of a common base and numeric suffix and you want all of them from some minimum suffix to some maximum suffix then you just need to two macro variables to how those two numbers to generate the code.

%let min=2030;
%let max=2032;
data want;
  set have ;
  success = .Z < x < max(of VAR_&min.-VAR_&max.);
run;

SAS Innovate 2025: Register Now

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!

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
  • 24 replies
  • 2111 views
  • 12 likes
  • 4 in conversation