I want to generate a series SAS code like:
data want;
length name label $ 200.;
name = "country='Country_A'";
label = "Country_A";
output;
name = "country='Country_B'";
label = "Country_B";
output;
name = "country='Country_C'";
label = "Country_C";
output;
*(...etc. so many countries, ie: Country_A to Country_Z);
run;
Is there a simple way in SAS to get there that I can generate such code just by typing less words.
Array?Macro? or any other methods?
Thanks in advance.
Best regards.
This is indeed a strange requirement, but easy to solve:
data want;
length
name label $ 200
i 8
;
drop i;
do i = rank('A') to rank('Z');
name = cats("country='Country_", byte(i), "'");
label = cats("Country_", byte(i));
output;
end;
run;
Why do you want to do this? Seems to be part of a bigger problem?
This is indeed a strange requirement, but easy to solve:
data want;
length
name label $ 200
i 8
;
drop i;
do i = rank('A') to rank('Z');
name = cats("country='Country_", byte(i), "'");
label = cats("Country_", byte(i));
output;
end;
run;
Then what defines your country names? Do you have them in a separate data set or?
Thanks you. I got what I want.
data want;
length name label $ 200 i 8;
array country[6] $100 ('Country A' 'Country B' 'Country C'
'Country D' 'Country E' 'Country F');
do i = 1 to 6;
name = cats("country='", country[i], "'");
label = cats(country[i]);
output;
end;
drop i country: ;
run;
Note that you can avoid creating actual columns in your dataset by declaring your array as temporary.
array country[6] $100 _TEMPORARY_ ('Country A' 'Country B' 'Country C'
'Country D' 'Country E' 'Country F')
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.