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')
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.