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

Hi can you help edit my code, I'm using below user defined format and informat to get my value in array, to store values of cards from "value" to "q" variable.

 

When i'm using declared varables array as shown below it is working fine, but when i'm changing it to unknown dimention it giving no data in the dataset.

 

array value{3} value1 value2 value3;
array q{3}$ q1 q2 q3;

 

*Informat;
Proc format;
invalue $ informat
1 = 'True'
2 = 'False'
3 = 'None'
4 = 'Invalid';
run;

*Format;
proc format;
value $ format
'True' = 101
'False' = 102
'None' = 103
'Invalid' = 104;
run;

data ds_user1;
infile cards;
retain id value1 value2 value3 q1 q2 q3;
informat value1 value2 value3 $ informat.;
input id$ value1 value2 value3;
array value{*} _numeric_;
array q{*} _character_;
do i=1 to dim(value);
	q{i} = value{i};
end;
format value1 value2 value3 $ format.;
drop i;
cards;
a 1 2 3
b 2 4 1
c 3 1 4
d 3 3 2
;
run;

 

Thanks in advance,

Ayushmaan

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, you can do it in a number of ways, for instance totalling how many there are from metadata and storing in a macro variable.  Personally I prefer to use Base SAS, in this case proc transpose.  Beinig able to move from normalised data to transposed data and back again is a fundamental modelling technique you should know, and by using this you can avoid the need to "know" how many elements up front, here is an example:

proc format;
  invalue $inf
  1 = 'True'
  2 = 'False'
  3 = 'None'
  4 = 'Invalid';
run;

*Format;
proc format;
  value $res
  'True' = 101
  'False' = 102
  'None' = 103
  'Invalid' = 104;
run;

data ds_user1;
  infile datalines;
  informat value1 value2 value3 $inf.;
  input id $ value1 value2 value3;
datalines;
a 1 2 3
b 2 4 1
c 3 1 4
d 3 3 2
;
run;

proc transpose data=ds_user1 out=t;
  by id;
  var value:;
run;

data t;
  set t;
  _name_=tranwrd(_name_,"value","q");
  col2=put(col1,$res.);
run;

proc transpose data=t out=norm;
  by id;
  var col2;
  id _name_;
  idlabel _name_;
run;

data want (drop=_name_);
  merge ds_user1 norm;
  by id;
run;

Do note I replaced he use of informat and format as names deliberately, it is really not a good idea to use reserved words for names. 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

@ab12nov

There are that many issues in your code that I really don't know where I would have to start.

 

Given your sample source data, why don't you tell us what you want to achieve. 

That's your sample data (Remark: OP changed data after I've posted below).

  datalines;
a 1 2 3b 
2 4 1c 3 
1 4d 3 3 2
;
run;

Please show us how your desired result should look like.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, you can do it in a number of ways, for instance totalling how many there are from metadata and storing in a macro variable.  Personally I prefer to use Base SAS, in this case proc transpose.  Beinig able to move from normalised data to transposed data and back again is a fundamental modelling technique you should know, and by using this you can avoid the need to "know" how many elements up front, here is an example:

proc format;
  invalue $inf
  1 = 'True'
  2 = 'False'
  3 = 'None'
  4 = 'Invalid';
run;

*Format;
proc format;
  value $res
  'True' = 101
  'False' = 102
  'None' = 103
  'Invalid' = 104;
run;

data ds_user1;
  infile datalines;
  informat value1 value2 value3 $inf.;
  input id $ value1 value2 value3;
datalines;
a 1 2 3
b 2 4 1
c 3 1 4
d 3 3 2
;
run;

proc transpose data=ds_user1 out=t;
  by id;
  var value:;
run;

data t;
  set t;
  _name_=tranwrd(_name_,"value","q");
  col2=put(col1,$res.);
run;

proc transpose data=t out=norm;
  by id;
  var col2;
  id _name_;
  idlabel _name_;
run;

data want (drop=_name_);
  merge ds_user1 norm;
  by id;
run;

Do note I replaced he use of informat and format as names deliberately, it is really not a good idea to use reserved words for names. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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