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')
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.