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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Why do you want to do this? Seems to be part of a bigger problem?

Dominus
Obsidian | Level 7
Cause this is a necessary data step to create a conditional judgement dataset for other calculation.
Dominus
Obsidian | Level 7
Or a simple way to create the want dataset
andreas_lds
Jade | Level 19

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;
Dominus
Obsidian | Level 7
Thank you for the answer, I'll try it. Actually, the country names are not a sequence.
PeterClemmensen
Tourmaline | Level 20

Then what defines your country names? Do you have them in a separate data set or? 

Dominus
Obsidian | Level 7
Not a separate data set. Maybe I can put them in a list or a data set.
Dominus
Obsidian | Level 7

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;
gamotte
Rhodochrosite | Level 12

@Dominus,

 

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') 
Dominus
Obsidian | Level 7
Thank you for the useful tips.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 10 replies
  • 672 views
  • 4 likes
  • 4 in conversation