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

Dear all,

I have a data set which contains a string variable specifying ranges of values. Some rows display the first and the last value of the range while others display a list of all possible values from the range, separated by a comma, like this:

 

Data have:

 

Var1
010-0100
020-050
010,025,048,061

 

Line by line, I would need to extract all possible values from the range to a different column, such as:

 

Data want:

 

Var1
010
020
030
040
050
060
070
080
090
0100
020
030
040
050
010
025
048
061

 

For ranges delimited by "-" the sequence increments always with a step of +10, e.g.: 010, 020, 030...

 

Can this transformation be achieved in SAS?

 

Many thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

@Matos  Is this what you're looking for?

data have;
    input var1 & $100.;
    datalines;
    010-0100  
    020-050  
    010,025,048,061  
    ;
run;

data want (keep=var2);
    set have;
    if find(var1, '-') > 1
        then do;
            range_start = substr(var1, 1, find(var1,'-') - 1);
            range_end = substr(var1, find(var1,'-') + 1);
            increment = input(range_start, best8.) + 10;

            var2 = range_start;
            output;

            do while (increment < range_end);
                var2 = put(increment, z3.);
                output;
                increment + 10;
            end;

            var2 = range_end;
            output;
        end;
    else if find(var1, ',')
        then do;
            do i=1 to countw(var1,',');
               var2 = scan(var1,i,',');
               output;
            end;
        end;
run;

proc print; run;

mklangley_0-1599600426427.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

@Matos wrote:

Dear all,

I have a data set which contains a string variable specifying ranges of values. Some rows display the first and the last value of the range while others display a list of all possible values from the range, separated by a comma, like this:

 

Data have:

 

Var1
010-0100
020-050
010,025,048,061

 

Line by line, I would need to extract all possible values from the range to a different column, such as:

 

Data want:

 

Var1
010
020
030
040
050
060
070
080
090
0100
020
030
040
050
010
025
048
061

 

For ranges delimited by "-" the sequence increments always with a step of +10, e.g.: 010, 020, 030...

 

Can this transformation be achieved in SAS?

 

Many thanks.


Yes but you need to answer a few questions to clarify what you actually want.

Are you wanting to create actual numeric values or character values?

Once your variable Var1 is created as character it will stay as such and numeric values would have be another variable.

Matos
Obsidian | Level 7
Sure I forgot to specify this point. The variable after transformation shall remain character.
mklangley
Lapis Lazuli | Level 10

@Matos  Is this what you're looking for?

data have;
    input var1 & $100.;
    datalines;
    010-0100  
    020-050  
    010,025,048,061  
    ;
run;

data want (keep=var2);
    set have;
    if find(var1, '-') > 1
        then do;
            range_start = substr(var1, 1, find(var1,'-') - 1);
            range_end = substr(var1, find(var1,'-') + 1);
            increment = input(range_start, best8.) + 10;

            var2 = range_start;
            output;

            do while (increment < range_end);
                var2 = put(increment, z3.);
                output;
                increment + 10;
            end;

            var2 = range_end;
            output;
        end;
    else if find(var1, ',')
        then do;
            do i=1 to countw(var1,',');
               var2 = scan(var1,i,',');
               output;
            end;
        end;
run;

proc print; run;

mklangley_0-1599600426427.png

 

Matos
Obsidian | Level 7

Absolutely! Thanks a lot for your help mklangley.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 790 views
  • 1 like
  • 3 in conversation