BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Zeus_Olympus
Fluorite | Level 6

Dear all,

 

I have two datasets A and B.

Dataset A with the following variable names

Name | Area |   Code   |  Num1  |  Num2  | Num3   | Num4   | Num5 |   Num6 |   Num7 |
AfGi    NY      A2134    34,57    45,67   89,00     43,78    56,76    23,45    53,23
LmKo    SR      G3897    94,36    23,89   73,23     26,87    12,20    34,50    78,98
  ..           ..       ....
 ..           ..        ....

where Name, Area and Code are Character variables
and Num1, Num2, Num3, Num4, Num5, Num6, Num7 are Numeric variables

 

Dataset B has two character variables with names Criter and Type and looks like:

 Criter    Type
  Num1     RED
  Num2     RED
  Num3     RED
  Num4     GREEN
  Num5     GREEN
  Num6     GREEN
  Num7     GREEN

 

I need to split table A into two Tables
A_RED and
A_GREEN
based on the combination of Criter and Type values.

 

Table A_RED

Name | Area |   Code   |  Num1  |  Num2  | Num3    |  Type
AfGi    NY      A2134     34,57    45,67   89,00      RED
LmKo    SR      G3897     94,36    23,89   73,23      RED 
..    ...    ...
and 
Table a_RED

Name | Area |   Code   |  Num4   |  Num5  |  Num6  |   Num7 |   Type
AfGi    NY      A2134     43,78     56,76    23,45    53,23     GREEN
LmKo    SR      G3897     26,87     12,20    34,50    78,98     GREEN
.. ... ....

I would very much appreciate your guidance.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you want to generate code from your B dataset.

First let's make some sample data.

data b;
  input varname :$32. Type :$20.;
cards;
Num1 RED
Num2 RED
Num3 RED
Num4 GREEN
Num5 GREEN
Num6 GREEN
Num7 GREEN
;

data a;
  input Name $ Area $  Code $ num1-num7;
cards;
AfGi NY A2134 34.57 45.67 89.00 43.78 56.76 23.45 53.23
LmKo SR G3897 94.36 23.89 73.23 26.87 12.20 34.50 78.98
;

Now let's use B to generate some code.

proc sort data=b;
  by type varname;
run;

filename code temp;
data _null_;
  file code ;
  set b end=eof;
  by type;
  if _n_=1 then put 'data';
  if first.type then put ' a_' type '(keep=name area code ' type @;
  put varname @;
  if last.type then put 'rename=(' type '=type) where=(' type= $quote. '))' ;
  if eof then put ';' ;
run;

data _null_;
  file code mod;
  set b end=eof;
  by type;
  if first.type then put 'length ' type '$20.;' type '=' type :$quote. ';' ;
run;

Now let's use that code. We can just add a SET statement to tell where the data is coming from.

%include code / source ;
  set a;
run;

SAS Log

856  %include code / source ;
NOTE: %INCLUDE (level 1) file CODE is file C:\Users\...\#LN00152.
857 +data
858 + a_GREEN (keep=name area code GREEN Num4 Num5 Num6 Num7 rename=(GREEN =type) where=(Type="GREEN" ))
859 + a_RED (keep=name area code RED Num1 Num2 Num3 rename=(RED =type) where=(Type="RED" ))
860 +;
861 +length GREEN $20.;GREEN ="GREEN" ;
862 +length RED $20.;RED ="RED" ;
NOTE: %INCLUDE (level 1) ending.
863    set a;
864  run;

NOTE: There were 2 observations read from the data set WORK.A.
NOTE: The data set WORK.A_GREEN has 2 observations and 8 variables.
NOTE: Compressing data set WORK.A_GREEN increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: The data set WORK.A_RED has 2 observations and 7 variables.
NOTE: Compressing data set WORK.A_RED increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Results:

Tom_0-1656435800752.png

 

 

View solution in original post

3 REPLIES 3
pink_poodle
Barite | Level 11
You would need to load critter and type into a hash and use that as a reference (i.e., look-up table). Here is an example:
https://support.sas.com/kb/47/224.html#:~:text=Load%20a%20SAS%20data%20set%20into%20a%20Hash,step%20....
Tom
Super User Tom
Super User

Sounds like you want to generate code from your B dataset.

First let's make some sample data.

data b;
  input varname :$32. Type :$20.;
cards;
Num1 RED
Num2 RED
Num3 RED
Num4 GREEN
Num5 GREEN
Num6 GREEN
Num7 GREEN
;

data a;
  input Name $ Area $  Code $ num1-num7;
cards;
AfGi NY A2134 34.57 45.67 89.00 43.78 56.76 23.45 53.23
LmKo SR G3897 94.36 23.89 73.23 26.87 12.20 34.50 78.98
;

Now let's use B to generate some code.

proc sort data=b;
  by type varname;
run;

filename code temp;
data _null_;
  file code ;
  set b end=eof;
  by type;
  if _n_=1 then put 'data';
  if first.type then put ' a_' type '(keep=name area code ' type @;
  put varname @;
  if last.type then put 'rename=(' type '=type) where=(' type= $quote. '))' ;
  if eof then put ';' ;
run;

data _null_;
  file code mod;
  set b end=eof;
  by type;
  if first.type then put 'length ' type '$20.;' type '=' type :$quote. ';' ;
run;

Now let's use that code. We can just add a SET statement to tell where the data is coming from.

%include code / source ;
  set a;
run;

SAS Log

856  %include code / source ;
NOTE: %INCLUDE (level 1) file CODE is file C:\Users\...\#LN00152.
857 +data
858 + a_GREEN (keep=name area code GREEN Num4 Num5 Num6 Num7 rename=(GREEN =type) where=(Type="GREEN" ))
859 + a_RED (keep=name area code RED Num1 Num2 Num3 rename=(RED =type) where=(Type="RED" ))
860 +;
861 +length GREEN $20.;GREEN ="GREEN" ;
862 +length RED $20.;RED ="RED" ;
NOTE: %INCLUDE (level 1) ending.
863    set a;
864  run;

NOTE: There were 2 observations read from the data set WORK.A.
NOTE: The data set WORK.A_GREEN has 2 observations and 8 variables.
NOTE: Compressing data set WORK.A_GREEN increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: The data set WORK.A_RED has 2 observations and 7 variables.
NOTE: Compressing data set WORK.A_RED increased size by 100.00 percent.
      Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Results:

Tom_0-1656435800752.png

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 514 views
  • 2 likes
  • 3 in conversation