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

The values of a variable -var1 of my data set  changes every month.

I am interested to create a macro variable for each unique value of the variable -Var1.....This time I have 5 different (unique) values of Var1. The macro variable &NumUni counts the number of unique values of Var1.

Here is my attempt!

The data set have contains a row for each unique value of Var1.

%MACRO LOOP;
          %DO I=1 %TO %sysevalf(&NumUni);
              DATA _NULL_;
               SET Have;
               IF _N_=&I  THEN CALL SYMPUTX('X&I',Var1);
               RUN;

          %END;
%MEND LOOP;

In this case NumUni=5, so I should be creating 5 macro variables X1, X2,X3,X4 and X5.

There is some thing wrong with SYMPUTX('X&I',Var1);

Any help would be highly appreciated!

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
shivas
Pyrite | Level 9

Hi,

Try this..you can also use count(name) into a macro variable.

%let NumUni=10;

proc sql;

select distinct name into :var1-:var&NumUni from sashelp.class;

quit;

Thanks,

Shiva

View solution in original post

4 REPLIES 4
shivas
Pyrite | Level 9

Hi,

Try this..you can also use count(name) into a macro variable.

%let NumUni=10;

proc sql;

select distinct name into :var1-:var&NumUni from sashelp.class;

quit;

Thanks,

Shiva

Reeza
Super User

Your code seems correct, except macro variables only resolve inside double quotes not single quotes.

You also don't need macro code, here's a datastep solution that assumes you have a sorted dataset and there are variations to handle unsorted data but you don't have to know the number of records ahead of time.

data _null_;

    set sashelp.class;

    by name;

    count=0;

    if first.name then count+1;

    call symput("x"||compress(count), name);

run;

%put &x1;

%put &x12;

rnmishra
Calcite | Level 5

Thanks all of you for your help.

For any  beginner like this one, the following material provides some extra tips.

SAS(R) 9.3 SQL Procedure User's Guide

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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