DATA Step, Macro, Functions and more

Using unknown array dimention to store value from one array to another

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Using unknown array dimention to store value from one array to another

[ Edited ]

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

 


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 7,401

Re: Using unknown array dimention to store value from one array to another

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


All Replies
Respected Advisor
Posts: 3,887

Re: Using unknown array dimention to store value from one array to another

[ Edited ]

@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.

Solution
2 weeks ago
Super User
Super User
Posts: 7,401

Re: Using unknown array dimention to store value from one array to another

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. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 106 views
  • 2 likes
  • 3 in conversation