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
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.;
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.
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.
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.;
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: