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

Hello everybody!

I was wondering if somebody could help me with my problem.

Basically I have a SAS macro variable which is a list of the variables that are present in a SAS data set and I am cycling through those variables and would like to say cap those variables that are > 254.

Here is the sample code

..... blah-blah-blah... the list of the variable names are put into the sas macro variable called modelvars.

%macro get_capped();

data a;

set b (obs=100);

%let j = 1;

%do %until (%scan(&modelvars.,&j,%str( ))= %str());

   %let var=%upcase(%scan(&modelvars.,&j,%str( )));

     %if &var. > 254 %then &var. = 254;

    %let j = %eval(&j +1);

%end;

run;

%mend get_capped;

%get_capped;

The RED code is the one that I am stuck on. I know that the variable name resolved to TEXT, but how do I make SAS compiler treat that text as if it was the name of the actual variable in the data set and then cap that variable?

Any help would be greatly appreciated!

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

On the one hand, you are very close to having a working program.  Getting rid of two % should make it work:

if &var. > 254 then &var. = 254;

On the other hand, this could be solved much more simply, without the need to define a macro.  Consider:

data a;

   set b (obs=100);

   array cap_me {*} &modelvars.;

   do _i_=1 to dim(cap_me);

       if cap_me{_i_} > 254 then cap_me{_i_} = 254;

   end;

run;

Good luck.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

On the one hand, you are very close to having a working program.  Getting rid of two % should make it work:

if &var. > 254 then &var. = 254;

On the other hand, this could be solved much more simply, without the need to define a macro.  Consider:

data a;

   set b (obs=100);

   array cap_me {*} &modelvars.;

   do _i_=1 to dim(cap_me);

       if cap_me{_i_} > 254 then cap_me{_i_} = 254;

   end;

run;

Good luck.

roman_makordey
Calcite | Level 5

Thank you very much for your help!

Linlin
Lapis Lazuli | Level 10

Hi,

try the code below:

%macro get_capped();

data a;

set b (obs=100);

%let j = 1;

%do %until (%scan(&modelvars.,&j,%str( ))= %str());

   %let var=%upcase(%scan(&modelvars.,&j,%str( )));

    if &var. > 254 then &var. = 254;

    %let j = %eval(&j +1);

%end;

run;

%mend get_capped;

%get_capped;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 3 replies
  • 538 views
  • 3 likes
  • 3 in conversation