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

Hi, 

i have a dataset with 100 obs and one variable of city.

how can i assign all the values to a macro varible using callsymput?

 

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

Do you mean a macrovariable per observation ? If so, you can suffix macrovariable names with row index.

 

data _NULL_;
    set cities;
    call symput(cats("city_",_N_), city);
run;

%put &city_10. &city_42.;

If you want a macrovariable listing all values of the same variable :

 

proc sql noprint;
    SELECT city
    INTO :lst_cities SEPARATED BY ','
    FROM cities;
quit;

%put &lst_cities.;

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

So this is quite unclear what you want to do, and it would be nice if you showed up (for a simple example of say 3 observations) what macro variables you want. 

 

Let me ask ... do you want one macro variable that contains ALL 100 of the city names? Or do you want 100 macro variables, each containing a single city name?

 

Its also possible that limiting the solutions to CALL SYMPUT is not a good decision. It may also be true that you don't even need macros, depending on what you want to do next.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You don't need to, if you have data in a dataset, then there are simpler, more efficient methods of coding using that than using macro.  For example, one often used ones is for where clauses, so avoid all that nasty macro, convert spaces to commas, ensure correct quoting etc. and simply do:

proc sql;
  select *
  from   adataset
  where city in (select city from 100obsdataset);
quit;

With that its simple coding, and the dataset can shrink/expand as needed.

gamotte
Rhodochrosite | Level 12

Hello,

 

Do you mean a macrovariable per observation ? If so, you can suffix macrovariable names with row index.

 

data _NULL_;
    set cities;
    call symput(cats("city_",_N_), city);
run;

%put &city_10. &city_42.;

If you want a macrovariable listing all values of the same variable :

 

proc sql noprint;
    SELECT city
    INTO :lst_cities SEPARATED BY ','
    FROM cities;
quit;

%put &lst_cities.;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 701 views
  • 0 likes
  • 4 in conversation